Yair Nevet
Yair Nevet

Reputation: 13003

jsTree - Get the selected node on loaded.jstree event

How can I get the selected node on the loaded.jstree event?

what should I do in the event handler:

    $('#Tree').bind('loaded.jstree', function(event, data){//TODO: How to get the selected node here?}).jstree();

By the way, I found out that the event data arg object contains a function called get_selected() but couldn't get anything from it.

My purpose is to redirect the client to the current selected node (by 'url' attribute).

Thanks in advance

Upvotes: 4

Views: 12632

Answers (2)

Himanshu Pathak
Himanshu Pathak

Reputation: 193

you can select current node by :

$('#' + data.node.id)

Code becomes:

$('#Tree').bind('loaded.jstree', function(event, data){
console.log($('#' + data.node.id)); //This is current node, see on console
}).jstree();

Upvotes: 0

Guillaume Cisco
Guillaume Cisco

Reputation: 2945

Seems according to the documentation of the demo here :

http://www.jstree.com/demo

you can do :

.one("reselect.jstree", function (event, data) { });

or

.bind("select_node.jstree", function (event, data) {  
                // `data.rslt.obj` is the jquery extended node that was clicked 
                alert(data.rslt.obj.attr("id")); 
            })

Read carefully the documentation as :

one is used, this is because if refresh is called those events are triggered

// 1) if using the UI plugin bind to select_node
        .bind("select_node.jstree", function (event, data) { 
            // `data.rslt.obj` is the jquery extended node that was clicked
            alert(data.rslt.obj.attr("id"));
        })
        // 2) if not using the UI plugin - the Anchor tags work as expected
        //    so if the anchor has a HREF attirbute - the page will be changed
        //    you can actually prevent the default, etc (normal jquery usage)
        .delegate("a", "click", function (event, data) { event.preventDefault(); })

For the last event delegate, instead of writing event.preventDefault();, you can make your redirection correctly if you're not using the UI plugin, and write : window.location = $(this).attr('href');

Upvotes: 2

Related Questions