Jump to content

Looping through child elements?


davej

Recommended Posts

I am trying to use getElementsByTagName() more to be more efficient but I am having some issues. Why can't I read the class in the following code?

<!DOCTYPE html><html><head><meta charset="utf-8"><title>getElementsByTagName</title><style></style> <script>window.onload = function() {assignfn();}function assignfn() {var forms = document.getElementsByTagName('form');alert('forms: ' + forms.length);for (var i=0 ; i<forms.length ; i++) {  forms[i].reset();  if (forms[i].name == 'form2') {	var inputs = forms[i].getElementsByTagName('input');	alert('inputs: ' + inputs.length);	for (var j=0 ; j<inputs.length ; j++) {	  alert('type:'+ inputs[j].type +' name:'+ inputs[j].name +' id: '+ inputs[j].id +' value:'+ inputs[j].value +' class:'+ inputs[j].class);	  if (inputs[j].type == 'radio') {		if (inputs[j].class == 'c2'){		  inputs[j].onclick = fnCorrect;		}else{		  inputs[j].onclick = fnNotCorrect;		}	  }	}  }}}//end of assignfunction fnCorrect() {alert('Correct!');}function fnNotCorrect() {alert('Not Correct!');}</script></head><body><p>Form1:</p><form name="form1"><input type="text" id="user"/></form><p>Form2:</p><form name="form2"><input type="text" id="acct"/><br/><input type="text" id="pwd"/><br/><input type="radio" name="f2g1" id="f2r1" value="1"/>1<br/><input type="radio" name="f2g1" id="f2r2" class="c2" value="2"/>2<br/><input type="radio" name="f2g1" id="f2r3" class="c3" value="3"/>3<br/><input type="radio" name="f2g1" id="f2r4" class="c4" value="4"/>4<br/></form><p>Form3:</p><form name="form3"><input type="text" id="num"/></form></body></html>

Link to comment
Share on other sites

Thanks! I think I have been spoiled by the IDE GUI's that give you a popup select box for all the available member functions. Is there perhaps a way to do that for Javascript? Thanks.

Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...