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:
HTML HTMLCollection length
It 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
NodeList
object is a a list (collection) of nodes extracted from a document. - A
NodeList
object is almost the same as anHTMLCollection
object. - Some(older) browsers return a
NodeList
object instead of anHTMLCollection
for methods likegetElementsByClassName()
- All browsers return a
NodeList
object for the propertychildNodes
. - Most browsers return a
NodeList
object for the methodquerySelectAll()
The following code selects all <p>
nodes in a document:
Accessing the size of the NodeList
is the same as before:
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.