Remove a DOM Element in JavaScript

June 20, 2020

Consider we have a div with 3 span

<div id="parent">
    <span id="child-1">1</span>
    <span id="child-2">2</span>
    <span id="child-3">3</span>

To remove an element you can use remove method on the node

let child2 = document.getElementById("child-2");
child2.remove(); //removes the second span element

If we call remove method on parent then all child node will also deleted along with parent

let parent = document.getElementById("parent");
parent.remove(); // deletes all node

Removing body of the webpage

let body = document.body;

Please donate here, for making more tutorials.