HTML

HTMLCollection

HTMLCollection

An array-like collection (list) of HTML elements is known as an HTMLCollection. A collection's elements can be accessed using the index (starts at 0). The length Property returns the collection's element count. Historically, this interface is known as HTMLCollection (before the modern DOM, collections implementing this interface could only have HTML elements as their items).

Name Description Example
length Returns the number of elements in an HTMLCollection let number = document.getElementsByTagName("p").length;
item() Returns the element at a specified index const collection = document.getElementsByTagName("p").item(0); let text = collection.innerHTML;
namedItem() Returns the element with a specified id const collection = document.getElementsByTagName("p"); const element = collection.namedItem("myElement"); let text = element.innerHTML;

Browser Compatibility

Element Chrome Edge Firefox Safari Opera
HTMLCollection Yes Yes Yes Yes Yes