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 »

Description

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.




Syntax

HTMLCollection.length

Return Value

Type Description
NumberThe 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

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.