FriendFX
FriendFX

Reputation: 3079

How to specify multiple event types in D3.js

Reading D3's documentation on event handling, I think it should be possible to specify multiple event types in the .on() call that should be handled by the same callback, like so:

d3.select("body").append("input").attr("type", "text")
  .on("click keypress", function () {
    console.log("fired "+d3.event);
  });

Try this on CodePen

In this example, I would expect both click and keypress events to cause the "fired" message to be printed to the console, but it doesn't work. If only one of the events is specified, it does for that single event type.

Am I misunderstanding the documentation or am I doing something else wrong?

Upvotes: 0

Views: 471

Answers (1)

FriendFX
FriendFX

Reputation: 3079

As noted by GerardoFurtado (Thanks for that!), specifying multiple event types is a new feature in version 4 of D3, but my CodePen used version 3 instead.

To fix it, use https://d3js.org/d3.v4.min.js instead of https://d3js.org/d3.v3.min.js in the HTML source.

Upvotes: 2

Related Questions