HTMLCollection lengthProperty
Definition and Usage
The length property returns the number of elements in an HTMLCollection object.
This property is read-only.
This property is useful when looping through an HTMLCollection object.
Syntax
HTMLCollection.length
Return Value
Returns a number representing the number of elements in the HTMLCollection object.
Browser Support
| Property | |||||
|---|---|---|---|---|---|
| length | Yes | Yes | Yes | Yes | Yes |
Examples
Examples
Loop through all elements with class="myclass" and modify their background colors:
var x = document.getElementsByClassName("myclass");
for (i = 0; i < x.length; i++) {
x.item(i).style.backgroundColor = "red";
}
Try it Yourself »
Related Articles
HTMLCollection: item()Method
HTML Element: getElementsByClassName()Method
HTML Element: getElementsByTagName()Method
Other Extensions
DOM HTMLCollection