What are NodeLists, and how do they work?
📣 Sponsor
Did you know that Javascript does not class a selection of multiple elements as an array? Instead, it is something called a NodeList
. A node list is essentially a list of elements. To generate a NodeList
, we can do something like this:
let myNodeList = document.querySelectorAll('p');
The above code will return a list of all paragraphs found on a page as a NodeList
.
Node lists are interesting because they aren't arrays, so they do not inherit all of the different functions that arrays have. One notable example is that, in some older browsers, such as Internet Explorer, NodeList
s do not inherit the foreach
function.
As such, if you wanted to add an event listener to every paragraph, the following code would throw an error in Internet Explorer:
let myNodeList = document.querySelectorAll('p');
myNodeList.forEach(function(item) {
item.addEventListener('click', function(e) {
// Do some click events
});
// For each node item..
});
Since this works in most modern browsers, you usually don't have to worry about using this, but if you want to support older browsers and use forEach
, we have to throw our NodeList into an array, like so:
let myNodeList = document.querySelectorAll('p');
Array.prototype.forEach.call(myNodeList, function(item) {
item.addEventListener('click', function(e) {
// Do some click events
});
// For each node item..
});
A little complicated, but now we can ensure all our users can access the event listeners we add to our NodeList items.
What functions do NodeLists support?
Since this article has focused on how NodeList
s haven't always had forEach
by default, you may be wondering what functions can be run on a NodeList. There are 5:
- NodeList.entries - returns an iterator for getting both the id and element as an id/element pair, i.e.
[ 1, p ]
. - NodeList.forEach - for iterating through each item individually.
- NodeList.item - for getting a specific item by id, i.e. get the first paragraph by
NodeList.item(0)
. - NodeList.keys - returns an iterator for getting keys, i.e. 1 2 3 4 5 ...
- NodeList.values - returns an iterator for getting the HTML elements, i.e. p p p p ...
It is worth noting that NodeList.item
is the only function which is supported by Internet Explorer. The rest are not.
To show you some examples, here is how we would run these functions on our NodeList:
NodeList.entries
let myNodeList = document.querySelectorAll('p');
// entries
let allEntries = myNodeList.entries();
for(var i of allEntries) {
// Console logs each paragraph with an id individually, such as [ 0, p ] [ 1, p ] [ 2, p ] ...
console.log(i);
}
NodeList.forEach
let myNodeList = document.querySelectorAll('p');
// forEach - iterate over each item
myNodeList.forEach(function(item) {
// Console logs each paragraph element individually
console.log(i);
});
NodeList.item
let myNodeList = document.querySelectorAll('p');
// item - get the first element (0)
let firstElement = myNodeList.item(0);
// Console logs the first element only
console.log(firstElement);
NodeList.keys
let myNodeList = document.querySelectorAll('p');
let getKeys = myNodeList.keys();
// Console logs the id of each element, i.e. 1 2 3 4 5 ...
for(var i of getKeys) {
console.log(i);
}
NodeList.values
let myNodeList = document.querySelectorAll('p');
let getValues = myNodeList.values();
// Console logs each HTML element as an array, i.e. p p p p ...
for(var i of getValues) {
console.log(i);
}
More Tips and Tricks for Javascript
- A Guide to Heaps, Stacks, References and Values in Javascript
- Waiting for the DOM to be ready in Javascript
- Making a Morphing 3D Sphere in Javascript with Three.js
- How Promises and Await work in Javascript
- How to do Everything with Javascript Arrays
- How to remove a specific item from an array
- Javascript innerHTML
- How to get the last element of an Array in Javascript
- Javascript Records and Tuples
- Javascript Variables