I made a put a click listener on an svg element using d3:
<svg height=100 width=100>
<circle cx=50 cy=50 r=40 stroke=black stroke-width=3 fill=red id=a>
<circle cx=50 cy=50 r=40 stroke=black stroke-width=3 fill=red id=b>
<circle cx=50 cy=50 r=40 stroke=black stroke-width=3 fill=red id=c>
</svg>
Javascript:
d3.select('svg').on('click', function(d, i) {
// Somehow console.log the ID of the circle clicked on (if any).
});
I'd like to refer to the circle that was clicked on (if any) in the handler. However, d3 does not give me a reference to the event. I know that I could just put a listener on every circle and not rely on the event bubbling to the <svg>
element, but I don't want to do that since I plan to dynamically add more circles later and don't want to have to carefully add and remove listeners.