Search code examples
jqueryjstree

How to get node by ID in Jstree


I have created a jstree and i have a problem with getting node by id of jstree. when i use get_node, i get an error:

TypeError: $(...).jstree.get_node is not a function

this is html code:

<div style="height: 75%; margin: 0; width: 100%;">
                    <div id="dashboardTree" style="border: 0; height: 99%; margin: 0; margin-top: 2px; overflow: auto; width: 99%;">
                    </div>
                </div>

this is the javascript:

$(document).ready(function () {
initDashboardArchiveTree();//Initial tree
var node = $('#dashboardTree').jstree(true).get_node('1')//get that error
});

How to get node by id in jsTree?What's wrong with this code?


Solution

  • Try this:

    var node = $('#dashboardTree').jstree(true).get_node('1, true')
    

    New addition: true

    OR

    Change this:

    var node = $('#dashboardTree').jstree(true).get_node('//something')
    

    To this:

    var node = $('#dashboardTree').jstree(true).find('//something');
    

    Get the JSON of the parent and find the children.

    Read the documentation on jstree/JSON.