I want to have a toggle button in an svg shape, and scale it down when the button is clicked, then scale up when clicked again, firstly I added a class collapse
like this. I want to remove the class if it has a class collapse
g.append("circle")
.classed("collapse", true)
.attr("cx", 0)
.attr("cy", 0)
.attr("r", radius * 0.4 - 10)
.attr("fill", color(radius * 0.4 - 10))
.on("click", (d) ->
if(hasClass("collapse")) # HOW DO I DO THIS
)
You can do this with the DOM API:
g.append("circle")
.classed("collapse", true)
.attr("cx", 0)
.attr("cy", 0)
.attr("r", radius * 0.4 - 10)
.attr("fill", color(radius * 0.4 - 10))
.on("click", (d) ->
if(this.classList.contains("collapse")) {
// ...
// this.classList.remove('collapse')
} else {
// ...
// this.classList.add('collapse')
}
)
Or jQuery:
g.append("circle")
.classed("collapse", true)
.attr("cx", 0)
.attr("cy", 0)
.attr("r", radius * 0.4 - 10)
.attr("fill", color(radius * 0.4 - 10))
.on("click", (d) ->
if($(this).hasClass("collapse")) {
// ...
}
)
The this
inside the call backs refers to the DOM Element.
This is, however, not quite the D3-isque way of doing things. One should save the collapsed
status on the data
associated with the node and act on it rather than save the status in the class
of the DOM elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With