JavaScript HTML DOM Collections
The HTMLCollection Object
The method getElementsByTagName() returns a HTMLCollection object. An HTMLCollection object is an array-like-list (collection) of HTML elements.
Consider the following code:
const myCollection = document.getElementsByTagName("p");
// to access the second <p> element, we can
myCollection[1]
// we access the indexes of the array as we would with a conventional arrayHTML HTMLCollection length
myCollection.lengthIt is important to note that while the collection may look like an array, it is not. While we can loop through it using index notation, array methods will not work on them.
JavaScript HTML DOM Node Lists
- A
NodeListobject is a a list (collection) of nodes extracted from a document. - A
NodeListobject is almost the same as anHTMLCollectionobject. - Some(older) browsers return a
NodeListobject instead of anHTMLCollectionfor methods likegetElementsByClassName() - All browsers return a
NodeListobject for the propertychildNodes. - Most browsers return a
NodeListobject for the methodquerySelectAll()
The following code selects all <p> nodes in a document:
const myNodeList = document.querySelectorAll("p");
// to access the second p element, we can write
myNodeList[1]Accessing the size of the NodeList is the same as before:
myNodelist.length.HTMLCollection VS. NodeList
So, both of these are very similar. They are both array-like containers. They both have the length method and both cannot call normal array methods.