Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make double click event on node in d3.js?

Tags:

svg

d3.js

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";;}) 
like image 592
JonghoKim Avatar asked Nov 17 '13 13:11

JonghoKim


2 Answers

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

nodes.on("dblclick",function(d){ alert("node was double clicked"); }); 
like image 192
toshi Avatar answered Sep 19 '22 22:09

toshi


If using D3 Observable:

const nodeEnter = node.enter().append("g")
        .on("dblclick", d => {
          d3.event.preventDefault();
          // do your thing
        });
like image 22
Cybernetic Avatar answered Sep 20 '22 22:09

Cybernetic