Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of jquery .empty() in d3?

Tags:

jquery

d3.js

In jQuery I can clear/empty a div with the following:

$("#graph").empty()

so the above command would clear/empty the following div if it had any tags inside it <div id="graph"></div>.

Is there an equivalent way to do this is d3?

see my PLUNKER here

Snippet of Plunker code here:

<div id="mylist">hello test1</div>


<div id="graph">hello test2</div>

<script>        
        $("#graph").empty() // comment out to not empty the div
</script>        

EDIT1

PLUNKER for my reference based on answer below.

like image 359
HattrickNZ Avatar asked Dec 14 '22 02:12

HattrickNZ


2 Answers

The official way to do this is to use .html(null), as in

d3.select("#graph").html(null);
like image 123
serv-inc Avatar answered Dec 21 '22 10:12

serv-inc


You have two options:

You could use .html("") and basically wipe the html content of the element you have selected.

Or if you want to delete a specific d3 selection you can go for .remove().

The remove method will remove the selection itself, whereas emptying out the html just takes care of all child nodes.

So for your case probably d3.select('#graph').html("").

I modified your Plunker to use d3: https://plnkr.co/edit/V2tYVxOTbzMUX3D7m57L?p=preview

like image 43
Anton v B Avatar answered Dec 21 '22 09:12

Anton v B