What are NodeLists, and how do they work?

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 myNodeLis…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson

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, NodeLists 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:

To convert our NodeList into a more manageable array, we can use the following code:

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 so far on how NodeLists haven't always had forEach by default, you may be wondering what functions can be run on a NodeList. There are 4:

  • 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.

As an example, 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);
}


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-10-20T20:22:25+00:00) What are NodeLists, and how do they work?. Retrieved from https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/

MLA
" » What are NodeLists, and how do they work?." Johnny Simpson | Sciencx - Thursday October 20, 2022, https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/
HARVARD
Johnny Simpson | Sciencx Thursday October 20, 2022 » What are NodeLists, and how do they work?., viewed ,<https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/>
VANCOUVER
Johnny Simpson | Sciencx - » What are NodeLists, and how do they work?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/
CHICAGO
" » What are NodeLists, and how do they work?." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/
IEEE
" » What are NodeLists, and how do they work?." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/. [Accessed: ]
rf:citation
» What are NodeLists, and how do they work? | Johnny Simpson | Sciencx | https://www.scien.cx/2022/10/20/what-are-nodelists-and-how-do-they-work/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.