HTMLCollection length Property
Example
Find out how many P elements there are in the document:
function myFunction() {
var l = document.getElementsByTagName("P").length;
alert(l);
}
Try it Yourself »
Definition and Usage
The length()
property returns the number of elements in a HTMLCollection.
This property is read-only.
This element is useful when you want to loop through a HTMLCollection.
Browser Support
Property | |||||
---|---|---|---|---|---|
length | Yes | Yes | Yes | Yes | Yes |
Syntax
HTMLCollection.length
Return Value
A Number, representing the number of elements in a HTMLCollection.
More Examples
Example
Loop through all elements with class="myclass", and change their background color:
var x = document.getElementsByClassName("myclass");
for (i = 0; i <
x.length; i++) {
x[i].style.backgroundColor = "red";
}
Try it Yourself »
Related Pages
HTMLCollection: item Method
HTML Element: getElementsByClassName() Method
HTML Element: getElementsByTagName() Method