Javascript DOM HTML document forms Collection

Introduction

Find out how many <form> elements there are in the document:

var x = document.forms.length;

Click the button to display the number of form elements in the document.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  First Name: <input type="text" name="fname" value="CSS"><br>
  Last Name: <input type="text" name="lname" value="HTML">
</form>//from   w  w w . j  a v  a  2  s  .co  m
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  var x = document.forms.length;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The forms collection returns a collection of all <form> elements in the document.

The elements in the collection are sorted as they appear in the source code.

Properties

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

This property is read-only

Methods

Method
[index]

Description
Get the <form> element from the collection with the specified index starting at 0
Returns null if the index number is out of range
item(index)

Returns the <form> element from the collection with the index starting at 0.
Returns null if the index number is out of range
namedItem(id)

Returns the <form> element from the collection with the specified id.
Returns null if the id does not exist



PreviousNext

Related