How to access all <form> elements within the current document in JavaScript

1 Answer

0 votes
<!DOCTYPE html>        
<html>
<head>
</head>
<body>

<form id="aaa_id">
    <input type="button" onclick="alert(document.forms[0].id);" value="aaa form" />
</form>

<form id="bbb_id">
    <input type="button" onclick="alert(document.forms[1].id);" value="bbb form" />
</form>

<form id="ccc_id">
    <input type="button" onclick="alert(document.forms[2].id);" value="ccc form" />
</form>
<script type="text/JavaScript">   

for (var i = 0; i < document.forms.length; i++) 
    document.write(document.forms[i].id + "<br />");

/*
run:

aaa_id
bbb_id
ccc_id

*/

</script>
</body>
</html>

 



answered Jun 9, 2016 by avibootz
...