Usually, in JavaScript, we listen to a single event from a single element like this:
const button = document.querySelector('button');
button.addEventListener('click', event => {
// do something...
})
But what if you need to listen to the same event from multiple elements. Well, we can solve this problem with a simple forEach loop:
const buttons = document.querySelectorAll('button');
buttons.forEach(btn => {
btn.addEventListener('click', event => {
// do something...
})
})