Javascript Reference - HTML DOM forms Collection








The forms collection returns an array of all the forms in the current document.

Syntax

document.forms[].property

Browser Support

forms Collection Yes Yes Yes Yes Yes

Example 1

The following code shows how to return the number of forms in the document.


<!DOCTYPE html>
<html>
<body>
<!--from  w w  w  .  j  a  v a  2 s.c o m-->
<form name="Form1"></form>
<form name="Form2"></form>
<form></form>

<p>Number of forms:
<script>
document.write(document.forms.length);
</script></p>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the name of the first form in the document.


<!DOCTYPE html>
<html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form></form>
<!--   w  ww. ja va 2 s. c  om-->
<p>Name of first form:
<script>
document.write(document.forms[0].name);
</script></p>

</body>
</html>

The code above is rendered as follows: