Javascript-HTML - how to iterate over all forms on a page?

How can I iterate over all forms in a document using javascript?

+5
source share
3 answers

The code below will go through an html document, receive all forms and display a pop-up warning about the names of each form.

var formsCollection = document.getElementsByTagName("form");
for(var i=0;i<formsCollection.length;i++)
{
   alert(formsCollection[i].name);
}

This is only the beginning if you want to get the desired result. After that, remove the warning and continue to do what you need.

+8
source

you can use

document.forms collection

See forms collection

+12
source

document.forms getElementsByTagName().

getElementsByTagName(), ( , ).

var formsCollection;
var r;

formsCollection=document.forms;

for(r=0;r<formsCollection.length;r++)
{
    alert(formsCollection[r].action);
}

This can be minimized, and of course the popup has changed to something useful, but I tried to keep it simple.

And for reference, here are some links to additional information:

+1
source

All Articles