26

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

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
NewToJS
  • 2,011
  • 4
  • 35
  • 62

1 Answers1

46

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.

GabeIsman
  • 821
  • 7
  • 9