47

I want to make double click event on nodes. So I tried

.on("dbclick",function(d){return "http://google.com");});

and

.bind({"dbclick",function(d){alert("hello")} });

But all failed. Can anyone help me?

Full codes are below.

var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("g")
    .attr("class", "node")
    //.on("dbclick",function(d){return "http://google.com");});
    //.attr("xlink:href", function(d){return d.url;}
    .call(force.drag);
    //.bind({"dbclick",function(d){alert("hello")} });

Finally, I used a below method. (dblclick also works)

var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("a") 
              .attr("class", "node") .attr("target", "_blank")
              .attr("xlink:href", function(d){return "google.com";;}) 
JonghoKim
  • 1,815
  • 6
  • 20
  • 42

2 Answers2

80

You can use "dblclick" instead of "dbclick":

nodes.on("dblclick",function(d){ alert("node was double clicked"); });
Isaac Lyman
  • 2,159
  • 1
  • 22
  • 40
toshi
  • 2,537
  • 1
  • 15
  • 12
1

If using D3 Observable:

const nodeEnter = node.enter().append("g")
        .on("dblclick", d => {
          d3.event.preventDefault();
          // do your thing
        });
Cybernetic
  • 11,188
  • 15
  • 76
  • 114