Javascript Add Event Listener to Multiple Elements

If you’ve ever worked in vanilla Javascript, you might be familiar with adding event listeners to elements using the following formula:

let element = document.querySelector(‘#button’);

element.addEventListener(‘click’, () => {
console.log(‘s…

If you’ve ever worked in vanilla Javascript, you might be familiar with adding event listeners to elements using the following formula:

let element = document.querySelector('#button');

element.addEventListener('click', () => {
    console.log('some event content here...')
})

The above code will, of course, trigger a function which fires when #button is fired. Sometimes, though, you need to add an event listener to multiple elements – say, every button that exists on a page. You might have found that even if you have multiple elements on a page, the above approach only adds your event to one element – the first one. What gives?

The issue is addEventListener is only good for adding an event to one DOM element – and querySelector only matches one element too. So how do you add an event listener to multiple elements on a page? Let’s look at the solution.

Adding Event Listeners to Multiple Elements

Instead of using querySelector, we’re going to use querySelectorAll to match all elements on our page. The following code returns an item of type NodeList, consisting of all DOM elements matching .button. To add events to every element, we’re going to need to loop through every matched element from our querySelector, and add events to each:

let elements = document.querySelectorAll('.button');

Javascript is weird because it doesn’t return DOM elements as a simple array – it returns them as a NodeList. If you want to learn about NodeLists in more detail, read my guide on that here.

In modern browsers, NodeLists behave a lot like arrays, so we can use forEach to loop through each. To add an event to each .button then, we need to loop through it using forEach. So adding a click event to all .button elements looks like this:

let elements = document.querySelectorAll('.button');

elements.forEach((item) => {
    item.addEventListener('click', () => {
        console.log('some event content here...')
    })
});

However, in older browsers like Internet Explorer, forEach doesn’t exist on NodeLists. Although this is not an issue in the modern day, you may find code where the result of querySelectorAll is changed into an array and looped through. This achieves the same thing, but it means that we are looping through arrays, not NodeLists.

let elements = document.querySelectorAll('.button');
Array.prototype.forEach.call(elements, (item) => {
    item.addEventListener('click', function(e) {
        console.log('some event content here...')
    });
});

Print Share Comment Cite Upload Translate
APA
Johnny Simpson | Sciencx (2024-03-29T11:46:04+00:00) » Javascript Add Event Listener to Multiple Elements. Retrieved from https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/.
MLA
" » Javascript Add Event Listener to Multiple Elements." Johnny Simpson | Sciencx - Thursday October 20, 2022, https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/
HARVARD
Johnny Simpson | Sciencx Thursday October 20, 2022 » Javascript Add Event Listener to Multiple Elements., viewed 2024-03-29T11:46:04+00:00,<https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/>
VANCOUVER
Johnny Simpson | Sciencx - » Javascript Add Event Listener to Multiple Elements. [Internet]. [Accessed 2024-03-29T11:46:04+00:00]. Available from: https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/
CHICAGO
" » Javascript Add Event Listener to Multiple Elements." Johnny Simpson | Sciencx - Accessed 2024-03-29T11:46:04+00:00. https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/
IEEE
" » Javascript Add Event Listener to Multiple Elements." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/. [Accessed: 2024-03-29T11:46:04+00:00]
rf:citation
» Javascript Add Event Listener to Multiple Elements | Johnny Simpson | Sciencx | https://www.scien.cx/2022/10/20/javascript-add-event-listener-to-multiple-elements/ | 2024-03-29T11:46:04+00:00
https://github.com/addpipe/simple-recorderjs-demo