question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong mouse event targets with d3.js v6

See original GitHub issue

hello, i couldn’t find any specific d3 module for events or mouse but i suppose this belong here. The changes with d3 events in v6 are creating some problems, i noticed this change of behavior which i think is an issue.

I have a table with images contained in the cells <td><img> and a callback on mouseover.

d3 v5.16

    tbody.selectAll("td:nth-child(2)")
        .on("mouseover", function () {  console.log(this, d3.event); // my handler follows...
         });

I get an event fired on the td item consistently. If the mouse goes over the image, i still get a target td with fromElement = img.

d3 v6.0

Now if i rewrite with the new API:

    tbody.selectAll("td:nth-child(2)")
        .on("mouseover", (event, d) => { console.log('mouseover', event.target); })

I would now get events fired both on td and img which does not seem logic and requires extra code to get around this problem. I only declared an event handler on td, why do i get events on img? I expect it to be as before, only fire on td. If the mouse is over the image the event should have fromElement = img.

Does it make sense?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Filcommented, Aug 28, 2020

I realize now you’re mentioning event.target; you could probably use event.currentTarget instead (I’ve updated the notebook). ((But again, this has not changed from D3@5.))

There are two events because when mouseover happens on IMG it bubbles up.

1reaction
herrviggcommented, Aug 28, 2020

I realize now you’re mentioning event.target; you could probably use event.currentTarget instead

Right. If you’re looking for a replacement for this in an arrow function, you want event.currentTarget, not event.target. The latter is different when an event bubbles, but event.currentTarget always refers to the element you’re listening to.

Yes! It works with event.currentTarget, awesome! I wasn’t aware of this difference, many thanks!!

Also, you want to use a mouseenter event if you just want to listen to the td and not its descendants.

In my case i want mouseover. Now it’s solved, everything works with v6 as before, that’s great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

d3 v6 pointer function not adjusting for scale and translate
Now I am replacing the d3.mouse function with d3.pointer. In my double click event I get the mouse position by calling d3.pointer(event) ....
Read more >
D3 6.0 migration guide - Observable
D3 6.0 migration guide · Moving to ES2015 · Event management · Brush, drag, zoom events · Mouse, touch, touches · d3.nest becomes...
Read more >
D3.js Tutorial: Mouse Events Handling - OctoPerf
Learn how to handle mouse events using D3.js to manipulate SVG graphics. d3.datum() and d3.mouse() explained via code and live examples.
Read more >
D3.js 6 documentation - DevDocs
D3.js 6.7.0 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Read more >
Using D3.js v6 with React - LogRocket Blog
See what you can do with D3.js v6 in your React apps. ... circle's stroke in the changeStroke function, and we call it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found