davej Posted March 30, 2013 Share Posted March 30, 2013 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 More sharing options...
jeffman Posted March 30, 2013 Share Posted March 30, 2013 Because the JavaScript property is className not class. Link to comment Share on other sites More sharing options...
Don E Posted March 30, 2013 Share Posted March 30, 2013 For this: inputs[j].class == 'c2', try instead: if(inputs[j].className == 'c2') Link to comment Share on other sites More sharing options...
davej Posted March 30, 2013 Author Share Posted March 30, 2013 (edited) 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 March 30, 2013 by davej Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now