21

I've been trying to get the text of a node that is selected in a jsTree. I am able to populate the tree and trigger the onSelect event, but I can't find out which node was clicked. I've seen examples on the net that use data.rslt.obj.attr("data") to fetch the text, however this is returning undefined for me. Additionally, when I try to get the selected node using .jstree('get_selected') I can't find the node text anywhere in the object. How can I get the node text?

Here is my onSelect callback function:

function onSelect(event, data)
{
    // Get the name of the equipment that was selected.
    var selected_node = $("#equipment_tree").jstree('get_selected');
    var equipment_name = data.rslt.obj.attr("data");
}

5 Answers 5

52

Update in 2018.

Thanks to @ProfK's comment, the API has changed in new version of jstree. In jstree v3.1.0 (or earlier), the API has changed to:

$("#treeContainer").on(
        "select_node.jstree", function(evt, data){
            //selected node object: data.node;
        }
);

For jstree old version (before 2013).

You can get the selected node object and its text by:

$("#treeContainer").bind(
        "select_node.jstree", function(evt, data){
            //selected node object: data.inst.get_json()[0];
            //selected node text: data.inst.get_json()[0].data
        }
);
5
  • 2
    Ah ha, this worked. An even shorter version I found is data.inst.get_text(). Commented Apr 19, 2012 at 13:45
  • this event seems to only fire for the parent, when that node is selected, but does not fire for each of the children that get selected. In my case using $("#treeNode").jstree().get_selected(true); as mikus mentions helps.
    – JGeerWM
    Commented Jan 13, 2017 at 16:38
  • @JGeerWM If you want only the child elements and not the parent, then you can use $("#treeNode").jstree().get_bottom_selected(true) I know its a late response.. But hope it helps someone Commented Nov 1, 2017 at 15:37
  • 1
    I found data.inst to be undefined, but I obtained the node simply through data.node.
    – ProfK
    Commented Apr 21, 2018 at 18:50
  • jQuery bind is deprecated, please use on instead.
    – wonsuc
    Commented Apr 8, 2019 at 4:32
19

jstree new version for get text from node should use data.node.text

$("#treeContainer").on("select_node.jstree",
     function(evt, data){
          alert(data.node.text);
     }
);
1
$("#equipment_tree").bind("select_node.jstree", function(evt, data){

             var i, j, r = [], ids=[];
                for(i = 0, j = data.selected.length; i < j; i++) {
                  r.push(data.instance.get_node(data.selected[i]).text);
                }
                alert('Selected: ' + r.join(', '));
           }
);

you have to try this.

1

With the current version it's best to use get_selected with full: true, which means that the method will return the full object(s), and not only id.

So for example:

$("#treeNode").jstree('get_selected', true);

or:

$("#treeNode").jstree().get_selected(true);

Each element in the array will have all the properties as text or id.

-1

The click event doesn't pass any data with it, so the event object will need to be used.

.bind("click.jstree", function (event) {
    alert($(event.currentTarget).parent("li:first").text().trim());
});
1
  • that would be really poor solution anyway
    – mikus
    Commented Jun 22, 2016 at 11:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.