Add removeElement
This commit is contained in:
18
snippets/removeElement.md
Normal file
18
snippets/removeElement.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
title: removeElement
|
||||||
|
tags: browser,beginner
|
||||||
|
---
|
||||||
|
|
||||||
|
Removes an element from the DOM.
|
||||||
|
|
||||||
|
- Use `Element.parentNode` to get the given element's parent node.
|
||||||
|
- Use `Element.removeChild()` to remove the given element from its parent node.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const removeElement = el => el.parentNode.removeChild(el);
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
removeElement(document.querySelector('#my-element'));
|
||||||
|
// Removes #my-element from the DOM
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user