Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct way to tell if my selection caught any existing elements

I'm making a toggle in d3, and trying to avoid global variables.

I can go ahead and select the item as though it was already in the scene:

d3.select('#awesome_line_graph') 

and then test to see if I caught anything using

if (d3.select('#awesome_line_graph')[0].every(function(d){return d===null})){     // draw awesome line graph } else {     d3.select('#awesome_line_graph').remove() } 

but this testing for the zeroth element for maybe more than one null thing seems terrible and hacky. How should I do it instead? Apologies for not knowing much javascript.

like image 764
Mike Dewar Avatar asked Mar 25 '12 03:03

Mike Dewar


1 Answers

Use selection.empty(). Also, if the selection is empty, there's no need to remove it.

like image 171
mbostock Avatar answered Sep 21 '22 01:09

mbostock