HTML DOM Document forms Property
- Previous page execCommand()
- Next page getElementById()
- Go Back to the Previous Level HTML DOM Documents
Definition and Usage
forms
The property returns a collection of all <form> elements in the document.
forms
The property returns HTMLCollection.
forms
The property is read-only.
See also:
Tip:Please use Form Elements Collection Returns all elements in the form.
HTMLCollection
HTMLCollection It is a similar array collection (list) of HTML elements.
可以通过索引访问集合中的元素(从 0 开始)。
length Elements in the collection can be accessed by index (starting from 0).
The property returns the number of elements in the collection.
Instance
Example 1
Returns the number of <form> elements in the document:
let num = document.forms.length;
Get the id of the first <form> element:
Example 2
Example 3
Get the id of the first <form> element:
let id = document.forms.item(0).id;
Example 4
Get the HTML content of the <form> element with id="myCarForm":
let html = document.forms.namedItem("myCarForm").innerHTML;
Example 5
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>"; }
Example 6
Use 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>"; }
Syntax
document.forms
Property
Property | Description |
---|---|
length | The number of elements in the collection. |
Method
Method | Description |
---|---|
[index] |
Returns the element at the specified index (starting from 0). Returns null if the index is out of range. |
item(index) |
Returns the element at the specified index (starting from 0). Returns null if the index is out of range. |
namedItem(id) |
Returns an element with the specified id element. If id Returns null if it does not exist. |
Return value
Type | Description |
---|---|
Object |
HTMLCollection object. All <form> elements in the document. They are sorted in the order they appear in the source code. |
Browser support
document.forms
It is a DOM Level 1 (1998) feature.
It is supported by all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Supports | 9-11 | Supports | Supports | Supports | Supports |
- Previous page execCommand()
- Next page getElementById()
- Go Back to the Previous Level HTML DOM Documents