Why when i log the tagName of all the nodes in a specific node list it gives me this repeating undefined feedback ?
This is the part of the code i’m extracting nodes from :
index.html
<form name="contactForm" id="contactForm">
<div id="textInfo">
<ul>
<li>
<label for="firstName" class="mainLabel">First Name : </label>
<input type="text" name="firstName" id="firstName"/>
<span>This must be filled</span>
</li>
<li>
<label for="lastName" class="mainLabel">Last Name : </label>
<input type="text" name="lastName" id="lastName"></input>
<span>This must be filled</span>
</li>
<li>
<label for="email" class="mainLabel">E-mail : </label>
<input type="email" name="email" id="email"></input>
<span>This must be filled</span>
</li>
</ul>
</div>
script.js
var myForm = document.forms["contactForm"];
eventUtil.add(myForm, "submit" , function(evt){
var firstName = myForm.elements["firstName"];
if(firstName.value == ""){
for(i=0; i < firstName.parentNode.childNodes.length ; i++){
console.log("childNodes[" + i + "]: " + firstName.parentNode.childNodes[i].tagName);
}
eventUtil.preventDefault(evt);
}
});
and the output is :
childNodes[0]: undefined
childNodes[1]: LABEL
childNodes[2]: undefined
childNodes[3]: INPUT
childNodes[4]: undefined
childNodes[5]: SPAN
childNodes[6]: undefined
Why does it give me that undefined output repeatedly ?
you can check nodeType (Node.ELEMENT_NODE == 1) to select Element only.