HTMLCollection length
Examples
How many paragraphs in the document:
let number = document.getElementsByTagName("p").length;
Try it Yourself »
Loop over elements with class="myclass" and change their font size:
const collection = document.getElementsByClassName("myclass");
for (let i = 0; i < collection.length; i++) {
collection[i].style.fontSize = "24px";
}
Try it Yourself »
Definition and Usage
The length
property returns the number of elements in an
HTMLCollection.
The length
property is read-only.
The length
property is useful when you want to loop through an HTMLCollection.
HTMLCollection
An HTMLCollection is an array-like collection (list) of HTML elements.
The elements in a collection can be accessed by index. The index starts at 0.
See Also:
Syntax
HTMLCollection.length
Return Value
Type | Description |
Number | The number of elements the HTMLCollection. |
Browser Support
HTMLCollection.length
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |