Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTMLCollection length property

HTMLCollection length property

DOM HTMLCollection Object Reference DOM HTMLCollection


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 indicating the number of elements in an HTMLCollection object.


Browser support

property
length Yes Yes Yes Yes Yes

Example

Example

Loop through and output all elements with class="myclass" and modify their background color:

var

x

=

document

.

getElementsByClassName

(

"

myclass

"

)

;

for

(

i

=

0

;

i

<

x

.

length

;

i

++

)

{

x

.

item

(

i

)

.

style

.

backgroundColor

=

"

red

"

;

}

Try it »


Related Articles

HTMLCollection: item() Method

HTML Element: getElementsByClassName() Method

HTML Element: getElementsByTagName() Method


DOM HTMLCollection Object Reference DOM HTMLCollection