Skip to content Skip to sidebar Skip to footer

Javascript: Why Won't This Cause All The Forms On A Page To Reset?

good evening fellow code bashers: i know it should be simple, but i'm tired and just want to get it done. this is how i thought it should be done, which is identical to how this f

Solution 1:

Try using reset()

var numberOfForms = document.getElementsByTagName("form");
console.log("numberOfForms: " + numberOfForms);
for (var i=0; i<numberOfForms.length; i++)
{
  document.forms[i].reset();
}

Solution 2:

Try this. You have already got an array with all HTML DOM Form elements. Use numberOfForms[i].reset() instead of trying to use document.forms[n].

var numberOfForms = document.getElementsByTagName("form"); 
console.log("numberOfForms: " + numberOfForms); 
for (var i=0; i<numberOfForms.length; i++) 
{ 
   numberOfForms[i].reset();
} 

Actually this is cleaner and more efficient, there is actually no need to traverse the DOM with document.getElementsByTagName()

var allForms = document.forms;
for (var i=0; i < allForms.length; i++) {
  allForms[i].reset();
}

Post a Comment for "Javascript: Why Won't This Cause All The Forms On A Page To Reset?"