Accepted answer

Your answer will work, but for posterity, these methods are more generic.

Remove all children from HTML:"div.parent").html("");

Remove all children from SVG/HTML:"g.parent").selectAll("*").remove();

The .html("") call works with my SVG, but it might be a side effect of using innerSVG.


To remove all element from a node:

var siblings = element.parentNode.childNodes;
for (var i = 0; i < siblings.length; i++) {
    for (var j = 0; j < siblings.length; j++) {


in this way, I have resolved it very easily,


and then I just re-added visual elements which were rendered differently because the code for calculating radius and color had changed properties. Thank you.


If you want to remove the element itself, just use element.remove(), as you did. In case you just want to remove the content of the element, but keep the element as-is, you can use f.ex.


instead of .html("") (I wasn't sure which element's children you want deleted). This keeps the element itself, but cleans all included content. It the official way to do this, so should work cross-browser.

PS: you wanted to change the circle sizes. Have you tried

d3.selectAll(".circle").attr("r", newValue);


My first advice is that you should read the d3.js API about selections:

You have to understand how the enter() command works (API). The fact that you have to use it to handle new nodes has a meaning which will help you.

Here is the basic process when you deal with

  • first you want to "attach" some data to the selection. So you have:

      var nodes = visualRoot.selectAll(".node")
  • Then you can modify all nodes each times data is changed (this will do exactly what you want). If for example you change the radius of old nodes which are in the new dataset you loaded

      nodes.attr("r", function(d){return d.radius})
  • Then, you have to handle new nodes, for this you have to select the new nodes, this is what selection.enter() is made for:

      var nodesEnter = nodes.enter()
          .attr("fill", "red")
          .attr("r", function(d){return d.radius})
  • Finally you certainly want to remove the nodes you don't want anymore, to do this, you have to select them, this is what selection.exit() is made for.

      var nodesRemove = nodes.exit().remove()

A good example of the whole process can also be found on the API wiki:

Related Query