DOM HTMLCollection Reference Manual
- Previous Page HTML Event Object
- Next Page HTML NodeList
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
Instance
Output the number of <p> elements in the document:
var x = document.getElementsByTagName("P"); document.write(x.length);
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>"); }
- Previous Page HTML Event Object
- Next Page HTML NodeList