Files
30-seconds-of-code/snippets/off.md
2018-01-05 23:36:02 +11:00

460 B

off

Removes an event listener from an element.

Use EventTarget.removeEventListener() to remove an event listener from an element.

const off = (el, evt, fn, opts = false) => el.removeEventListener(evt, fn, opts);
// See the `on` snippet.
const fn = () => console.log('!');
const ref = on(document.body, 'click', fn, { target: 'p' });
off(document.body, 'click', ref); // no longer logs '!' upon clicking a `p` el child of the body