Document forms Collection - Javascript DOM

Javascript examples for DOM:Document forms

Description

The forms collection returns a collection of all <form> elements sorted as they appear in the HTML code.

Properties

Property Description
length Returns the number of <form> elements in the collection.

Methods

MethodDescription
[index] <form> element with the specified index (starts at 0).
item(index) <form> element with the specified index (starts at 0).
namedItem(id) <form> element with the specified id.

Return Value:

An HTMLCollection Object, representing all <form> elements in the document.

The following code shows how to Find out how many <form> elements there are in the document:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  First name: <input type="text" name="fname" value="Donald"><br>
  Last name: <input type="text" name="lname" value="Duck"><br>
  City: <input type="text" name="fname" value="Duckburg"><br>
  <input type="submit" value="Submit">
</form>/*  ww w.ja v a 2 s  . co  m*/

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

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

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

</body>
</html>

Related Tutorials