DOM HTMLCollection Reference Manual

HTMLCollection object

The HTMLCollection object is a list of HTML elements similar to an array.

Methods like getElementsByTagName() return HTMLCollections.

Properties and Methods

The following properties and methods can be used on the HTMLCollection object:

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

Instance

Instance

Get the HTMLCollection:

var x = document.getElementsByTagName("P");
// Return the collection of all P elements in the document

Try It Yourself

Instance

Output the number of <p> elements in the document:

var x = document.getElementsByTagName("P");
document.write(x.length);

Try It Yourself

Instance

Loop through each element in the HTMLCollection:

x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i < l; i++) {
  document.write(x[i].tagName + "<br>");
}

Try It Yourself