'easily get text appended to a node on d3 graph
I can't get my head around the d variable conventionnally used in d3.
Once the graph is generated, i'd like to pass the text value as a variable on click (or at least log it on console).
const node = svg
.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 15)
.attr("stroke", "none")
.style("fill", "none")
const text = svg
.append("g")
.selectAll("text")
.data(nodes)
.join("text")
.text((d) => d.word)
.style("cursor", "pointer")
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle");
.on("click", (d) => console.log(/*value of text here as string*/))
node.append("title").text((d) => d.id);
I tried console.log(d) and it displays a whole load of infos without an easy path to the value of the clicked text.
Solution 1:[1]
I don't know if it's the fastest way but good enough I guess
(d.path[0].firstChild.data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tao77 |
