Sometimes, we want to attach ‘onclick’ event to D3 chart background with JavaScript.
In this article, we’ll look at how to attach ‘onclick’ event to D3 chart background with JavaScript.
How to attach ‘onclick’ event to D3 chart background with JavaScript?
To attach ‘onclick’ event to D3 chart background with JavaScript, we call the on
method.
For instance, we write
rect.on("click", () => {
console.log("rect");
d3.event.stopPropagation();
});
to call on
with 'click
to add a click listener to the rect
object.
In the event listener, we call stopPropagation
to stop the event from bubbling up.
Conclusion
To attach ‘onclick’ event to D3 chart background with JavaScript, we call the on
method.