0

I'm using d3 v4 and trying to apply a style to an element on mouseover. I'm wondering if it's an API change with V4 but I can't seem to get at the node

I have code:

.on('mouseover', () => {
  let self = d3.select(this);
  let c = self.attr('class');

but this gives an error

Cannot read property 'getAttribute' of null

So the d3.select() doesn't seem to work... The mouseover IS firing however.

I can use

.on('mouseover', (elem) => {
  console.log('elem', elem);

Which will give me some type of D3 object, but not a DOM node. I can't use any D3 methods on this object

  elem.classed("hilite", true);
  elem.attr("class", "hilite");

Neither of those methods exist on a d3 returned object.

So how do I do this super basic operation in d3?

related to Change class of one element when hover over another element d3

Community
  • 1
  • 1
dcsan
  • 11,333
  • 15
  • 77
  • 118

1 Answers1

-1

You can use arrow functions (if this an arrow function), if you don't need access to this of the current element. In this case you do need, because D3 calls event handlers setting this to the element triggering this event.

See: Using arrow functions with d3

Community
  • 1
  • 1
Nixie
  • 637
  • 4
  • 9