HTML DOM formsCollection
Document Object
Definition and Usage
The forms collection returns an array collection of all forms on the current page.
Syntax
document.forms[].property
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the forms collection.
Examples
Example 1
Returns the number of forms in the document:
<html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form></form>
<p>Number of forms:
<script>
document.write(document.forms.length);
</script></p>
</body>
</html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form></form>
<p>Number of forms:
<script>
document.write(document.forms.length);
</script></p>
</body>
</html>
Output of the above example:
Number of forms: 3
Try it yourself »
Example 2
Returns the name of the first form in the document:
<html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form></form>
<p>Name of the first form:
<script>
document.write(document.forms[0].name);
</script></p>
</body>
</html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form></form>
<p>Name of the first form:
<script>
document.write(document.forms[0].name);
</script></p>
</body>
</html>
Output of the above example:
Name of the first form: Form1
Try it yourself »
Document Object Other Extensions