DOM HTMLCollection
HTMLCollection is a collection of HTML elements.
The HTMLCollection object is similar to an array list containing HTML elements.
getElementsByTagName()The method returns an HTMLCollection object.
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 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. |
Example
Returns a collection of all p elements, which is an HTMLCollection object:
Count the number of p elements in the document:
Loop through and output all elements in the HTMLCollection object:
Example
var x, i, l;
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i < l; i++) {
document.write(x[i].tagName + "<br>");
}
Try it Yourself »