Remove DOM element

JavaScript, Browser · Jan 6, 2021

Removes an element from the DOM.

  • Use Node.parentNode to get the given element's parent node.
  • Use Node.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

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub or Twitter.

More like this