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

偶尔善良 提交于 2019-12-03 15:25:48

The code below will go through an html document, get all forms and do a pop-up alert of the names of each form.

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

This is just a start to see if you are getting the reult you require. Thereafter, remove the alert and continue to do what you need to.

rahul

You can use

document.forms collection

See forms Collection

Here's an example using the document.forms instead of getElementsByTagName().

As with the getElementsByTagName() example this goes through all the forms and does a popup alert with the action (instead of name, as it is more likely to be set).

var formsCollection;
var r;

formsCollection=document.forms;

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

This can be condensed down and of course the popup changed to something useful but I have tried to keep it simple.

And for reference here are some links to more info:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!