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
Theforms property returns a collection of all <form> elements in a document.
Theforms property returns anHTMLCollection.
Theforms property is read-only.
HTMLCollection
AnHTMLCollection 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
| Property | Description |
| length | The number of elements in the collection. |
Methods
| Method | Description |
| [index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
| item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
| namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
Return Value
| Type | Description |
| Object | An HTMLCollection Object. 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 theform.elements collection to getthe 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 |

