I'm trying to pass a value to a d3 click event bind to a group composed of a circle and a letter. However this value remains 'undefined' in the callback function. On the other hand if I pass the same value to another function of the same group, such as .text(d => d.value) that works fine. The issue is only with the click event. Please find my piece of code:
//The parent svg
var s = d3.select("#"+this.id) //this refers to the current object
.append("svg")
.attr("id",this.svgid)
.attr("width","100%")
.attr("height","100%");
//The value to be passed to the click event
const dd = [{value:"whatever"}];
var group1 = s.data(dd).append("svg:g");
//First element of the group = a circle
group1.append("svg:circle")
.attr("cx","85%")
.attr("cy","20%")
.attr("r","10px")
.attr("fill","#FFFFFF");
//Second element of the group = letter i
group1.append("svg:text").text("i")
.attr("x","85%")
.attr("y","25%")
.attr("fill","#555555");
group1.append("svg:text").text(d => d.value).attr("x","50%").attr("y","50%");//This line works fine
group1.on("click",d => console.log(d.value));//This line returns 'undefined'--> Why?
I tried several flavors of the callback function such function(d){console.log(d.value);} but without success. Any idea?
Replace
group1.on("click",d => console.log(d.value));
with
group1.on("click",(e, d) => console.log(d.value));
Since D3 V6, event is passed as the first argument to the mouse event callbacks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With