Files
30-seconds-of-code/snippets/off.md
Angelos Chalaris 4b4ed5c7ed Update off.md
2018-01-05 18:05:23 +02:00

17 lines
526 B
Markdown

### off
Removes an event listener from an element.
Use `EventTarget.removeEventListener()` to remove an event listener from an element.
Omit the fourth argument `opts` to use `false` or specify it based on the options used when the event listener was added.
```js
const off = (el, evt, fn, opts = false) => el.removeEventListener(evt, fn, opts);
```
```js
const fn = () => console.log('!');
document.body.addEventListener('click', fn);
off(document.body, 'click', fn); // no longer logs '!' upon clicking on the page
```