Find out how many elements there are in a specified <form> element:
The result of x will be:
More "Try it Yourself" examples below.
The elements collection returns a collection of all elements in a form.
Note: The elements in the collection are sorted as they appear in the source code.
Note: The elements collection returns all elements inside the <form> element, not all <form> elements in the document. To get all <form> elements in the document, use the document.forms collection instead.
Collection | |||||
---|---|---|---|---|---|
elements | Yes | Yes | Yes | Yes | Yes |
Property | Description |
---|---|
length | Returns the number of elements in the <form> element. Note: This property is read-only |
Method | Description |
---|---|
[index] | Returns the element in <form> with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
item(index) | Returns the element in <form> with the specified index
(starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the element in <form> with the specified id. Note: Returns null if the id does not exist |
DOM Version: | Core Level 2 Document Object |
---|---|
Return Value: | An HTMLFormsControlCollection Object, representing all elements in a <form> element. The elements in the collection are sorted as they appear in the source code |
[index]
Get the value of the first element (index 0) in a form:
The result of x will be:
item(index)
Get the value of the first element (index 0) in a form:
The result of x will be:
namedItem(id)
Get the value of the element with name="fname" in a form:
The result of x will be:
Loop through all elements in a form and output the value of each element:
The result of txt will be: