Nest all content into snippets
This commit is contained in:
24
snippets/js/s/off.md
Normal file
24
snippets/js/s/off.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
title: Remove event listener from element
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [browser,event]
|
||||
cover: mug-flower-book
|
||||
dateModified: 2020-10-21T21:54:53+03:00
|
||||
---
|
||||
|
||||
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
|
||||
```
|
||||
Reference in New Issue
Block a user