Remove DOM element

Angelos Chalaris · JavaScript, Browser · Jan 6, 2021

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.
const removeElement = el => el.parentNode.removeChild(el);
removeElement(document.querySelector('#my-element'));
// Removes #my-element from the DOM

Would you like to help us improve 30 seconds of code?Take a quick survey

Recommended snippets