690 B
690 B
title, tags, cover, firstSeen, lastUpdated
| title | tags | cover | firstSeen | lastUpdated |
|---|---|---|---|---|
| Add multiple listeners | browser,event | balloons | 2020-10-08T00:40:30+03:00 | 2020-10-22T20:23:47+03:00 |
Adds multiple event listeners with the same handler to an element.
- Use
Array.prototype.forEach()andEventTarget.addEventListener()to add multiple event listeners with an assigned callback function to an element.
const addMultipleListeners = (el, types, listener, options, useCapture) => {
types.forEach(type =>
el.addEventListener(type, listener, options, useCapture)
);
};
addMultipleListeners(
document.querySelector('.my-element'),
['click', 'mousedown'],
() => { console.log('hello!') }
);