JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
DOM Node lists
The HTML DOM NodeList Object
A NodeList object is a list (collection) of nodes that are extracted from a document.
A NodeList object is similar to HTMLCollection object.
Some (older) browsers return a NodeList object instead of an HTMLCollection for methods like getElementsByClassName().
All browsers return a NodeList object for the property childNodes.
Most browsers return a NodeList object for the method querySelectorAll().
The below code selects all <p> nodes in a document:
const myNodeList = document.querySelectorAll(“p”);
The elements in the NodeList are accessed by an index number.
To access the second <p> node use the below code:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p id=”demo”></p>
<script>
const myNodelist = document.querySelectorAll(“p”);
document.getElementById(“demo”).innerHTML = “The innerHTML of the second paragraph is: ” + myNodelist[1].innerHTML;
</script>
</body>
</html>
Output
JavaScript HTML DOM
Hello World!
Hello Norway!
HTML DOM Node List Length
The length property specifies the number of nodes in a node list:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hellow World!</p>
<p>Hellow Norway!</p>
<p id=”demo”></p>
<script>
const myNodelist = document.querySelectorAll(“p”);
document.getElementById(“demo”).innerHTML = “This document contains ” + myNodelist.length + ” paragraphs.”;
</script>
</body>
</html>
Output
JavaScript HTML DOM
Hello World!
Hello Norway!
This document contains 3 paragraphs.
The length property is used to loop through the nodes in a node list.
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p>Click the button to change the color of all p elements.</p>
<button onclick=”myFunction()”>Try it</button>
<script>
function myFunction() {
const myNodelist = document.querySelectorAll(“p”);
for (let i = 0; i < myNodelist.length; i++) {
myNodelist[i].style.color = “red”;
}
}
</script>
</body>
</html>
The Difference Between an HTMLCollection and a NodeList
A NodeList and an HTMLcollection are very much the same things.
- Both are array-like collections (lists) of nodes (elements) extracted from a document. The nodes can be accessed by index numbers. The index starts at 0.
- Both have a length property that returns the number of elements in the list (collection).
- An HTMLCollection is a collection of document elements.
- A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes).
- HTMLCollection items are accessed by their name, id, or index number.
- NodeList items can only be accessed by their index number.
- An HTMLCollection is always a live collection. Example: If you add a <li> element to a list in the DOM, the list in the HTMLCollection will also change.
- A NodeList is most often a static collection. Example: If you add a <li> element to a list in the DOM, the list in NodeList will not change.
- The getElementsByClassName() and getElementsByTagName() methods return a live HTMLCollection.
- The querySelectorAll() method returns a static NodeList.
- The childNodes property returns a live NodeList.