JavaScript loop querySelectorAll results

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thank you for sharing this Valuable Information. Definitely, we need to About it. I'm really happy to know about that, Yeah I'm super excited to share what I found! I have found Ezytor . EZYTOR is a drag-and-drop website builder. Developers can easily edit their websites with this editor. By using EZYTOR, you can create a website faster and with more control than with any other drag-and-drop tool. I'm happy to find this Great One! so You can try it If you want!🥰
Thank you for sharing this Valuable Information. Definitely, we need to About it. I'm really happy to know about that, Yeah I'm super excited to share what I found! I have found Ezytor. EZYTOR is a drag-and-drop website builder. Developers can easily edit their websites with this editor. By using EZYTOR, you can create a website faster and with more control than with any other drag-and-drop tool. I'm happy to find this Great One! so You can try it If you want!
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Let's talk about NodeLists, the magical results of a querySelectorAll() query.
It's not an array, but it looks and behaves like one. It can be tricky looping over these elements, and there are multiple ways of looping them.
How the selector looks like:
const items = document.querySelectorAll('li');
console.log(items);
The result:

This is by far the best supported method, if support as many browsers as possible this is the way to go.
for (let i = 0; i < items.length; i++) {
items[i].addEventListener('click', function() {
console.log(`Text = ${items[i].innerText}`);
});
}
It's just not the most modern or visible appealing method.
We can also use the for...of loop.
for (const item of items) {
item.addEventListener('click', () => {
console.log(`2: Text = ${item.innerText}`);
});
}
This is supported by all modern browsers and work pretty well.
My all-time favourite too loop NodeList elements is the forEach loop.
It's the most easy method, but will only work in modern browsers.
items.forEach(item => {
item.addEventListener('click', () => {
console.log(`3: Text = ${item.innerText}`);
});
});
This method can be extended by converting it to an array before.
[].forEach.call(items, function(item) {
item.addEventListener('click', function() {
console.log(`3.1: Text = ${item.innerText}`);
});
});
Or, we can use the spread operator to convert it into an array:
[...items].forEach(item => {
item.addEventListener('click', () => {
console.log(`3.2: Text = ${item.innerText}`);
});
});
There you go, three ways (+ iterations) on how to loop over nodeList elements.
You can also have a play around with this Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter