From 5e4f9c4b53f40ed4b747d88c419656a60de3c261 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Thu, 7 Jan 2021 00:20:34 +0200 Subject: [PATCH] Add removeElement --- snippets/removeElement.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/removeElement.md diff --git a/snippets/removeElement.md b/snippets/removeElement.md new file mode 100644 index 000000000..d140c5532 --- /dev/null +++ b/snippets/removeElement.md @@ -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 +```