HTML DOM Document forms
Example
Number of <form> elements in the document:
Get the id of the first <form> element:
Get the id of the first <form> element:
Get the HTML content of the <form> element with id="myCarForm":
More examples below.
Description
The forms property returns a collection of all <form> elements in a document.
The forms property returns an HTMLCollection.
The forms property is read-only.
HTMLCollection
An HTMLCollection is an array-like collection (list) of HTML elements.
The length Property returns the number of elements in the collection.
The elements can be accessed by index (starts at 0).
An HTMLCollection is live. It is automatically updated when the document is changed.
Syntax
Properties
Methods
Returns
null if the index is out of range.
Returns
null if the index is out of range.
Returns
null if the id does not exist.
Return Value
All <form> elements in the document.
Sorted as they appear in the source code
More Examples
Loop through all <form> elements and output the id of each form:
let text = "";
for (let i = 0; i < forms.length; i++) {
text += forms[i].id + "<br>";
}
Using the form.elements collection to get the value of each element in the form:
let text = "";
for (let i = 0; i < form.length; i++) {
text += forms.elements[i].value + "<br>";
}
Browser Support
document.forms is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | 9-11 |