I have the following function that is called whenever a text element is clicked. Once called, it selects an element via id, and then changes its opacity to either 0 or 1.
d3.select("#viz").selectAll("text").on("click", function() {
selectedCandidate = d3.select(this).attr("id").split(",")[0];
selectedColor = d3.select(this).attr("id").split(",")[1];
// determine if the line with same id as the text is visible
var active = document.getElementById(selectedCandidate).active ? false : true,
newOpacity = active ? 0 : 1;
newColor = active ? "gray" : selectedColor;
// hide or show the elements
d3.selectAll("#"+selectedCandidate)
.style("opacity", newOpacity);
})
That works exactly as expected. I now want to add in a transition so the opacity change is gradual. I expected the solution to look something like this:
// hide or show the elements
d3.select("#"+selectedCandidate)
.transition()
.duration(3000)
.style("opacity", newOpacity);
And that works in the console as expected, but inside the function, it stops working altogether. Instead, the opacity just stops changing when the function is evoked.