How to get the value of each element in the form from 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++)
    for (var j = 0; j < document.forms[i].length; j++)  
        document.write(document.forms[i].elements[j].value + "<br />");


/*
run:

aaa form
bbb form
ccc form

*/

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

 



answered Jun 9, 2016 by avibootz
...