Array.prototype.forEach()
and EventTarget.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!') } );
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️