What is considered best practice for attaching event handler to multiple elements at once

There are a great many ways to do this & so I am curious about which is considered the ‘best’ way.

An example using forEach:

const listItems = document.querySelectorAll('li');

function vanish() {
  this.classList.add('vanish');
}

listItems.forEach(li =>
  li.addEventListener('click', vanish));

An example using e.target:

const list = document.querySelector('ul');
list.addEventListener('click', (e) => {
e.target.classList.add('vanish');  
})

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.