24

How can I get the id of the selected node in a jsTree?

function createNewNode() {
  alert('test');
  var tree = $.tree.reference("#basic_html");
  selectedNodeId = xxxxxxxxx; //insert instruction to get id here
  tree.create({ data : "New Node Name" }, selectedNodeId);
}
Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
murze
  • 4,015
  • 8
  • 43
  • 70

14 Answers14

97

Unable to get harpo's solution to work, and unwilling to use Olivier's solution as it uses internal jsTree functions, I came up with a different approach.

$('#tree').jstree('get_selected').attr('id')

It's that simple. The get_selected function returns an array of selected list items. If you do .attr on that array, jQuery will look at the first item in the list. If you need IDs of multiple selections, then treat it as an array instead.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 2
    no idea why data.rslt.obj.attr("id") doesn't work here (if you have data as the argument in a onselect handler), but this does the trick and finishes a >1h search for a solution - thanks! – Nicolas78 Sep 01 '11 at 16:58
  • 4
    Definitely the proper solution. – davidethell Oct 14 '11 at 10:55
  • Depending on which ID you need (DOM or actual node data) try this instead: `$('#tree').jstree('get_selected').data('id')` – nickb Nov 06 '11 at 19:42
  • 3
    me this did not work as `.attr` is undefined. Would this be because jQuery is not loaded?. `$('#tree').jstree('get_selected')` is indeed returning an array of the id's of the nodes selected – Don Cheadle Sep 29 '15 at 18:47
  • 1
    Did not do the job, as mmcrae mentioned it is undefined. What did the trick for me is $('#tree').jstree('get_selected').toString(); – GTodorov Sep 06 '16 at 04:38
  • @GTodorov What's the return value from `get_selected`? It's doubtful you want a string here. – Brad Sep 06 '16 at 04:45
  • @PeterRankin If you're going to completely change an answer, just post your own answer. – Brad Dec 06 '16 at 17:42
  • 3
    In the most recent version of jsTree, the code `$('#tree').jstree('get_selected')` actually returns an array of IDs, not nodes. To get an array of nodes, use `$('#tree').jstree('get_selected', true)`. The answer above will not work in the most recent version of jsTree. See the [current documentation](https://www.jstree.com/api/#/?f=get_selected([full])) for more information. – Peter Rankin Dec 06 '16 at 18:11
  • @PeterRankin You should post that as your own answer, and indicate which the new API works for. This (very) old answer will work for people with old versions. – Brad Dec 06 '16 at 18:19
  • 1
    @Brad -- Thanks for your help. New answer for more recent API posted [here](http://stackoverflow.com/a/41002307/2186980). – Peter Rankin Dec 06 '16 at 18:28
  • why would you intentionally allow the answer to become outdated? the edit didn't even remove the original answer so wouldn't have been a problem for people still using an old version of the library – rdans Feb 15 '21 at 13:08
  • @rdans It's better to have a different answer posted as a different answer, to not confuse anyone on the version difference, and so that Peter's answer can (potentially) be accepted on the question. Peter wrote a good answer... it can stand alone. – Brad Feb 15 '21 at 17:53
11

Nodes in jsTree are essentially wrapped list items. This will get you a reference to the first one.

var n = $.tree.focused().get_node('li:eq(0)')

You can replace $.tree.focused() if you have a reference to the tree.

To get the id, take the first matched element

if (n.length)
    id = n[0].id

or you can use the jQuery attr function, which works on the first element in the set

id = n.attr('id');
harpo
  • 41,820
  • 13
  • 96
  • 131
11

In jstree version 3.1.1, you can get it directly from get_selected:

$("#<your tree container's id>").jstree("get_selected")
tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
  • Your answer is 100% correct. For others, the 'get_selected' method does not get the node, but instead gets the id of the node. You can get the node using the id by calling method 'get_node' - ex. var node = $("#myelement").jstree("get_node", nodeId); – barrypicker Feb 21 '18 at 17:58
5

In the most recent version of jsTree (checked at 3.3.3), you can do this to get an array of IDs:

var ids = $('#tree').jstree('get_selected');

This will return, for example, ["selected_id1", "selected_id2", "selected_id3"]. If you want to get the selected nodes (not IDs), you can do this:

var nodes = $('#tree').jstree('get_selected', true);

The current docs contain more information.

Peter Rankin
  • 713
  • 1
  • 6
  • 29
3
  $.jstree._reference('#my_tree_container')._get_node(null, true).each(function() {
    id = $(this).attr("id");
    alert('Id selected: ' + id);        
  });
1

I was having problems getting the selected ids from a tree with MULTIPLE selections. This is the way I got them:

var checked_ids = [];
$("#your-tree-id").jstree('get_selected').each(function(){    
      checked_ids.push($(this).data('id'));                         
});
agarcia
  • 63
  • 5
1

In my case, the data call doesnt work. I succeed in accessing my node data by using attr function.

$("#tree").jstree("get_selected").attr("my-data-name");
Damien C
  • 969
  • 1
  • 10
  • 16
1

to get all selected ids use the below code

var selectedData = [];
var selectedIndexes;
 selectedIndexes = $("#jstree").jstree("get_selected", true);
jQuery.each(selectedIndexes, function (index, value) {
     selectedData.push(selectedIndexes[index].id);
 });

now you have all the selected id's in the "selectedData" variable

Ali
  • 1,080
  • 16
  • 22
0

With the latest version of jsTree, you can do it as follows:

var checked_ids = [];
$('#your-tree-id').jstree("get_checked",null,true).each(function(){
    checked_ids.push(this.id);
});
alert(checked_ids.join(","));
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Jamshid Hashimi
  • 7,639
  • 2
  • 28
  • 27
0
<script type="text/javascript>
    checked_ids.push($(this).context.id);
</script>
George Brighton
  • 5,131
  • 9
  • 27
  • 36
harshboss
  • 276
  • 2
  • 9
0

Just use

var nodeId = $('#FaqTreeView').jstree().get_selected("id")[0].id;

where #FaqTreeView is the id of your div that contains the jstree.

Pang
  • 9,564
  • 146
  • 81
  • 122
0

In some cases and/or jstree versions this solution doesn't work.

$('#tree').jstree('get_selected').attr('id');

Instead of defined "id" I get nothing. What did the trick for me is:

$("#tree").jstree("get_selected").toString();
GTodorov
  • 1,993
  • 21
  • 24
0

These are all old answers for old versions. As of version 3.3.3 this will work to get all attributes of the selected node.

$('#jstree').jstree().get_selected(true)[0]

If you then want the id then add .id at the end. You can look at all the other attributes in web developer tools if you copy the above code.

Tom McDonough
  • 1,176
  • 15
  • 18
0

You can use the following code var nodes = $("#jstree_demo_div").jstree(true).get_selected("full", true);//List of selected node

nodes[0].id//Which will give id of 1st object from array