Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js check to see if a node has a class

Tags:

I'm trying to write a click function to select the next g.slice node, add a class of .sliceActive to it and remove .sliceActive from the original .slice. However, only when you are at the last g.slice (with a class of .slice5) you would add the .sliceActive to the the first g.slice with a class of .slice0.

This is what I have so far that is not working. I think the problem is I don't know how to see if the current .sliceActive node also has the class of .slice5.

        $(".next").click(function(){
            var nextSlice;
            if(d3.select("g.sliceActive").hasClass("slice5")){
                nextSlice= d3.select(".slice0");
            }else{
                nextSlice= d3.select("g.sliceActive + g");
            }               
            d3.select("g.sliceActive").classed("sliceActive",false);
            nextSlice.classed("sliceActive",true);
        });

And here is how it looks in the web inspector: enter image description here

like image 258
NewToJS Avatar asked Jun 03 '14 11:06

NewToJS


1 Answers

d3's classed function with no second parameter will return whether the selected element has the passed class.

d3.select("g.sliceActive").classed("slice5")

Should tell you what you need to know.

like image 63
GabeIsman Avatar answered Oct 05 '22 15:10

GabeIsman