Files
30-seconds-of-code/snippets/removeElement.md
2021-01-07 00:20:34 +02:00

19 lines
405 B
Markdown

---
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
```