Document forms Collection - Loop through all <form> elements in the document, and output the id of each form: - Javascript DOM

Javascript examples for DOM:Document forms

Description

Document forms Collection - Loop through all <form> elements in the document, and output the id of each form:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form id="myCarForm">
  Favorite Car: <input type="text" name="fname" value="Volvo">
</form>//from ww  w  .  j av  a 2 s . c  om

<form id="myColorForm">
  Favorite Color: <input type="text" name="favcolor" value="Blue">
</form>

<button type="button" onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.forms;
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt +  x[i].id + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials