Link to home
Start Free TrialLog in
Avatar of ray-solomon
ray-solomonFlag for United States of America

asked on

Onchange and Onblur clear textarea boxes

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.
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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()">



ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 0h4crying0utloud
0h4crying0utloud



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>
Avatar of ray-solomon

ASKER

That was exactly what I needed. Thanks Zvonko!