HTML DOM Document forms
Example
Number of <form> elements in the document:
let num = document.forms.length;
Try it Yourself »
Get the id of the first <form> element:
let id = document.forms[0].id;
Try it Yourself »
Get the id of the first <form> element:
let id = document.forms.item(0).id;
Try it Yourself »
Get the HTML content of the <form> element with id="myCarForm":
let html = document.forms.namedItem("myCarForm").innerHTML;
Try it Yourself »
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 elements in a collection can be accessed by index (starts at 0).
The length Property returns the number of elements in the collection.
Syntax
document.forms
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:
const forms = document.forms;
let text = "";
for (let i = 0; i < forms.length; i++) {
text += forms[i].id + "<br>";
}
Try it Yourself »
Using the form.elements collection to get the value of each element in the form:
const form = document.forms[0];
let text = "";
for (let i = 0; i < form.length; i++) {
text += forms.elements[i].value + "<br>";
}
Try it Yourself »
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 |