DOM HTMLCollection
HTMLCollection is a collection of HTML elements.
An HTMLCollection object is like an array list containing HTML elements.
The getElementsByTagName() method returns an HTMLCollection object.
Properties and Methods
The following table lists the properties and methods in the HTMLCollection object:
Property/Method | Description |
---|---|
item() | Returns the element at the specified index in the HTMLCollection. |
length | Returns the number of elements in the HTMLCollection. |
namedItem() | Returns the element in the HTMLCollection with the specified ID or name attribute. |
Examples
Returns the collection of all p elements, which is an HTMLCollection object:
Example1
var x = document.getElementsByTagName("p");
Count the number of p elements in the document:
Example2
var x = document.getElementsByTagName("P"); document.write(x.length);
Loop through all elements in an HTMLCollection object:
Example3
var x = document.getElementsByTagName("P"); document.write(x.length);