82

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

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50
Niyaz
  • 53,943
  • 55
  • 151
  • 182
  • `node.remove();` is the modern way to do this. `document.getElementById("FirstDiv").remove();` for your use case – Gibolt Nov 11 '16 at 08:21

9 Answers9

108

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.

Jason Bunting
  • 58,249
  • 14
  • 102
  • 93
  • 1
    By "poor implementations". Do you mean IE6, IE7, IE8 or something else? (in particular, do the good browsers leak memory too? I currently don't care anymore for IE 6-7.) – Camilo Martin Sep 13 '12 at 22:00
  • @CamiloMartin - not sure of the specifics these days, but as far as I recall, the 'good' browsers are supposed to watch for such things and handle it. YMMV – Jason Bunting Sep 23 '12 at 06:33
49

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);
}
c0deNinja
  • 3,956
  • 1
  • 29
  • 45
eplawless
  • 4,225
  • 7
  • 34
  • 35
21

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

Gibolt
  • 42,564
  • 15
  • 187
  • 127
  • Please edit to indicate what "previous method" is, as questions get voted up/down and removed this ends up without a definitive meaning – Mark Schultheiss Jul 29 '20 at 13:43
4

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

Polsonby
  • 22,825
  • 19
  • 59
  • 74
4

A jQuery solution

HTML

<select id="foo">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Javascript

// remove child "option" element with a "value" attribute equal to "2"
$("#foo > option[value='2']").remove();

// remove all child "option" elements
$("#foo > option").remove();

References:

Attribute Equals Selector [name=value]

Selects elements that have the specified attribute with a value exactly equal to a certain value.

Child Selector (“parent > child”)

Selects all direct child elements specified by "child" of elements specified by "parent"

.remove()

Similar to .empty(), the .remove() method takes elements out of the DOM. We use .remove() when we want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

Chris Jacob
  • 11,878
  • 7
  • 47
  • 42
3

Use the following code:

//for Internet Explorer
document.getElementById("FirstDiv").removeNode(true);

//for other browsers
var fDiv = document.getElementById("FirstDiv");
fDiv.removeChild(fDiv.childNodes[0]); //first check on which node your required node exists, if it is on [0] use this, otherwise use where it exists.
Abhishek
  • 2,925
  • 4
  • 34
  • 59
eagle
  • 567
  • 1
  • 6
  • 24
1
    var p=document.getElementById('childId').parentNode;
    var c=document.getElementById('childId');
    p.removeChild(c);
    alert('Deleted');

p is parent node and c is child node
parentNode is a JavaScript variable which contains parent reference

Easy to understand

Vivek Tiwari
  • 15
  • 1
  • 3
0

You should be able to use the .RemoveNode method of the node or the .RemoveChild method of the parent node.

EBGreen
  • 36,735
  • 12
  • 65
  • 85
0

You should probably use a JavaScript library to do things like this.

For example, MochiKit has a function removeElement, and jQuery has remove.

Mike Stone
  • 44,224
  • 30
  • 113
  • 140