Easy Tutorial
❮ Prop Audio Currentsrc Jsref Regexp Octal ❯

HTMLCollection length Property

DOM HTMLCollection


Definition and Usage

The length property returns the number of elements in an HTMLCollection object.

This property is read-only.

It 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

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.item(i).style.backgroundColor = "red";
}

Related Articles

HTMLCollection: item() Method

HTML Element: getElementsByClassName() Method

HTML Element: getElementsByTagName() Method


DOM HTMLCollection

❮ Prop Audio Currentsrc Jsref Regexp Octal ❯