How to listen for double-click on jstree?
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 ;)
Alex Reynolds
Bioinformaticist, hobbyist iPhone developer, pug caregiver
Updated on June 27, 2022Comments
-
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 aninput
field in a form somewhere.) -
Alex Reynolds about 13 yearsAfter
// Do some action
, if I addalert(data);
then I getnull
. -
Alex Reynolds about 13 yearsThis SO answer is close to yours and seems to work: stackoverflow.com/questions/3674625/…
-
TechMaze about 9 yearsnode.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