Categories
JavaScript Answers

How to attach ‘onclick’ event to D3 chart background with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *