How to listen for double-click on jstree?

12,583

Solution 1

I have used something like this way back a year ago, i don't know if there's any change in the current jstree version :

jstree.bind("dblclick.jstree", function (event) {
   var node = $(event.target).closest("li");
   var data = node.data("jstree");
   // Do some action
});

node : Contains the li that is being clicked.

data : Contains the metadata.

Solution 2

Nirmal's solution works if you click anywhere on the jstree div. I wanted to enable double click only on the nodes themselves and not, for example, on the whitespace to the right. changing the solution a little enabled this:

$('#jstree-div a').live('dblclick',function (e) {
    var node = $(e.target).closest("li");
    var type = node.attr('rel');
    var item = node[0].id;

    // do stuff...
});

Not sure why the 'rel' and the 'id' attributes are in different places in the resulting node, but it works ;)

Share:
12,583
Alex Reynolds
Author by

Alex Reynolds

Bioinformaticist, hobbyist iPhone developer, pug caregiver

Updated on June 27, 2022

Comments

  • Alex Reynolds
    Alex Reynolds about 2 years

    How would I write a listener for a double-click event on a jstree object? (For example, I'd like to double-click on a tree node and paste its anchor's href value into an input field in a form somewhere.)

  • Alex Reynolds
    Alex Reynolds about 13 years
    After // Do some action, if I add alert(data); then I get null.
  • Alex Reynolds
    Alex Reynolds about 13 years
    This SO answer is close to yours and seems to work: stackoverflow.com/questions/3674625/…
  • TechMaze
    TechMaze about 9 years
    node.data("jstree") is returning undefined in latest version ofjStree(3.1.x) So use this: var tree = $(this).jstree(); var node = tree.get_node(evt.target); var nodePath = tree.get_path(node).join("/"); So you have both node object and tree object to invoke any tree API