I'm trying to get d3 to change the color of plot points upon clicking them, but can't seem to get this working at the moment. The commented line below does change the color from white to magenta, but the toggleColor function does not seem to do anything. Actually, the alert only happens when first run, and not when a point is clicked. What am I doing wrong here?
var circle = graph.selectAll("circle.value") .data(data) .enter().append("circle") .attr("class", "value") .attr("cx", function(d) { return x(d.hour); }) .attr("cy", function(d) { return y(d.value); }) .attr("r", 5) //.on("click", function(){d3.select(this).attr("class", "flagged");}); .on("click", toggleColor); var toggleColor = (function(){ // throw in an alert for good measure. . . alert("Clicked?") var currentColor = "white"; return function(){ currentColor = currentColor == "white" ? "magenta" : "white"; d3.select(this).atrr("class", "flagged"); } })();