The length property specifies the number of nodes in a NodeList.
myNodelist.length |
The length property is useful for iterating through the nodes in a NodeList.
Modify the color of all <p> elements in a NodeList:
const myNodelist = document.querySelectorAll(“p”); for (let i = 0; i < myNodelist.length; i++) { myNodelist[i].style.color = “red”; } |
A NodeList and an HTMLCollection are quite similar:
length
property that returns the number of items in the collection.Key differences:
<li>
element is added to a list in the DOM, the HTMLCollection will automatically update.<li>
element to a list will not change the NodeList.getElementsByClassName()
and getElementsByTagName()
methods return live HTMLCollections.querySelectorAll()
method returns a static NodeList.childNodes
property returns a live NodeList.
Not an Array! A NodeList may appear similar to an array, but it is not an array. You can iterate over a NodeList and access its nodes by index. However, you cannot use array methods like push(), pop(), or join() on a NodeList. |