Q:

Write a JavaScript program to add an event listener to an element with the ability to use event delegation

0

Write a JavaScript program to add an event listener to an element with the ability to use event delegation.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const on = (el, evt, fn, opts = {}) => {
  const delegatorFn = e => e.target.matches(opts.target) && fn.call(e.target, e);
  el.addEventListener(evt, opts.target ? delegatorFn : fn, opts.options || false);
  if (opts.target) return delegatorFn;
};

const fn = () => console.log('!');
console.log(on(document.body, 'click', fn));  
console.log(on(document.body, 'click', fn, { target: 'p' }));  
console.log(on(document.body, 'click', fn, { options: true }));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now