How can I remove a child node in HTML using JavaScript?

Is there a function like document.getElementById("FirstDiv").clear()?


Solution 1:

To answer the original question - there are various ways to do this, but the following would be the simplest.

If you already have a handle to the child node that you want to remove, i.e. you have a JavaScript variable that holds a reference to it:

myChildNode.parentNode.removeChild(myChildNode);

Obviously, if you are not using one of the numerous libraries that already do this, you would want to create a function to abstract this out:

function removeElement(node) {
    node.parentNode.removeChild(node);
}

EDIT: As has been mentioned by others: if you have any event handlers wired up to the node you are removing, you will want to make sure you disconnect those before the last reference to the node being removed goes out of scope, lest poor implementations of the JavaScript interpreter leak memory.

Solution 2:

If you want to clear the div and remove all child nodes, you could put:

var mydiv = document.getElementById('FirstDiv');
while(mydiv.firstChild) {
  mydiv.removeChild(mydiv.firstChild);
}

Solution 3:

Modern Solution - child.remove()

For your use case:

document.getElementById("FirstDiv").remove()

This is recommended by W3C since late 2015, and is vanilla JS. All major browsers support it.

Mozilla Docs

Supported Browsers - 96% May 2020

Solution 4:

You have to remove any event handlers you've set on the node before you remove it, to avoid memory leaks in IE