Solved

Onchange and Onblur clear textarea boxes

Posted on 2006-07-07
4
729 Views
Last Modified: 2008-03-17
I need help on coding a javascript.

Within my form is 4 textfields and 16 textarea boxes beneath it.. The 16 textarea boxes are laid out into 4 goups with 4 textarea's in each group.

Here is suedo code of what I need it to do.

if onblur || onchange textfield 2 = "" { clear all boxes in goup 2 }
if onblur || onchange textfield 3 = "" { clear all boxes in goup 3 }
if onblur || onchange textfield 4 = "" { clear all boxes in goup 4 }

So for example, when someone clears textfield 2 then all four textareas in group 2 get cleared as well.
0
Comment
Question by:ray-solomon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 17061314
Like this:
<script>
function checkBlock(theField){
  if(theField.value==""){
    var theForm = theField.form;
    var ta = arguments;
    for(var i=1;i<ta.length;i++){
       theForm[ta[o]].value="";
    }
  }
}
</script>


And you call it like this:
<input type=text name="textfieldA" onChange="checkBlock(this,'textareaA1','textareaA2','textareaA3','textareaA4')" onBlur="this.onchange()">



0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 17061374
Sorry, the loop var is "i" not "o":

<script>
function checkBlock(theField){
  if(theField.value==""){
    var theForm = theField.form;
    var ta = arguments;
    for(var i=1;i<ta.length;i++){
       theForm[ta[i]].value="";
    }
  }
}
</script>

0
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 17061594


Here's another way using class names:


<input class="group1" type="text"><br>
<input class="group1" type="text"><br>
<input class="group1" type="text"><br>
X <input  type="text" onblur="if(this.value == ''){clearGroup('group1');}"><br>
<br><br><br>

<input class="group2" type="text"><br>
<input class="group2" type="text"><br>
<input class="group2" type="text"><br>
X <input  type="text" onchange="if(this.value==''){clearGroup('group2');}"><br>

<script>

function clearGroup(groupName) {
  var fieldGroup = document.getElementsByTagName("input");
  for (var i=0; i<fieldGroup.length; i++) {
        if (fieldGroup[i].className == groupName) {
              fieldGroup[i].value = "";
        }
      }
}

</script>
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17062106
That was exactly what I needed. Thanks Zvonko!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Provide the IDEA of External JS 1 38
Send form to asp server side 6 27
converting document.write() to innerHTML 7 48
what should i learn? javascript or typescript? 3 37
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question