Files
30-seconds-of-code/snippets/addEventListenerAll.md
Isah Abba Ibrahim 86d5f9b3fa Corrected function call
The function created isn't the function that was called. I corrected the function call
2021-07-29 22:46:36 +01:00

721 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
addEventListenerAll browser,event,intermediate 2021-04-22T08:53:29+03:00 2021-04-22T08:53:29+03:00

Attaches an event listener to all the provided targets.

  • Use Array.prototype.forEach() and EventTarget.addEventListener() to attach the provided listener for the given event type to all targets.
const addEventListenerAll = (targets, type, listener, options, useCapture) => {
  targets.forEach(target =>
    target.addEventListener(type, listener, options, useCapture)
  );
};
addEventListenerAll(document.querySelectorAll('a'), 'click', () =>
  console.log('Clicked a link')
);
// Logs 'Clicked a link' whenever any anchor element is clicked