D3 .on click - where is it coming from?

I am using the .on(‘click’, change); to call a function called change.
But I don’t know how to extract the svg element that was clicked.
Only the pointerEvent (event) and .data (which I’m not using) is parsed.
I can look at the event.target and see the rect object element when I console log it, but I can’t access the ‘id’ in it.

Possible solutions:
I would like, is to be able to parse a variable to the function (i variable to show which was clicked).

I would like to extract the ID from the eventpoint.target.

Or, used .data([0,1,2,3,4]) when constructing the svg, because my function would also gets the d was well as the event parsed.

I just worked out, i can use

this.id

Still if there are other ways to parse variables via the .on it would be great to know.
Also knowing that the .data() would work too would be helpful.

I think you want to use d3.select(this) to access the clicked element.

Thanks, that works too in a d3 friendly way for easy chaining.

1 Like