vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
20 lines (19 loc) • 615 B
JavaScript
import isEventTarget from './isEventTarget';
import eventOptionsSupported from './eventOptionsSupported';
function off(elm, eventNames, handler, options) {
if (!isEventTarget(elm)) {
options = handler;
handler = eventNames;
eventNames = elm;
elm = document;
}
const opts = eventOptionsSupported()
? options
: !!(options && options.capture);
if (!Array.isArray(eventNames)) {
eventNames = [eventNames];
}
eventNames.forEach((evt) => elm.removeEventListener(evt, handler, opts));
return elm;
}
export default off;