HTML CSS Bootstrap Javascript Icons Python
JavaScript Reference
Overview
JS Reference
JS Array Object JS Boolean Object JS Date Object JS Style Declaration Object JS Error Object JS Global Object JS Math Object JS Number Object JS String Object JS RegExp Object JS Statements
DOM Reference
DOM Attribute DOM Console DOM Document DOM Element DOM Events DOM Event Objects DOM History DOM HTMLCollection DOM Location DOM Navigator DOM Screen DOM Style DOM Window WEB Storage

DOM HTMLCollection Object

The HTMLCollection Object

The HTMLCollection object represents a generic collection (array-like object similar to arguments) of elements (in document order) and offers methods and properties for selecting from the list.

An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.

Methods like the getElementsByTagName() returns an HTMLCollection.

HTMLCollection Properties and Methods

The following table lists the properties and methods of the HTMLCollection object:

Property / Method Description
item() Returns the element at the specified index in an HTMLCollection
length Returns the number of elements in an HTMLCollection
namedItem() Returns the element with the specified ID, or name, in an HTMLCollection

Examples

This example returns an HTMLCollection:

Run code

Change the HTML content of the first <p> element of this document:

Run code

Find out how many <p> elements there are in the document:

Run code

Get the content of the <p> element with ID "demo":

Run code

A shorthand method can also be used, and will produce the same result:

Run code