Steve
Steve

Reputation: 923

jsTree can't find node by ID

I have no trouble getting the jsTree to implement on the page.

I tried SO MANY different things from so many suggestions on various sites.

$('#myTree').jstree({
....
})
.on('loaded.jstree', function (e, dta) {
    var t = $('#myTree').jstree(true);
    var n = t.get_node("myIDstring");
    console.log(t);
    console.log(n);
});

My last attempt before this post was to try using the .on(loaded.jstree) callback as shown above. But it didn't make any difference from where I tried to get the node. Always the same result.

console.log(t) proves I have a tree. In this case I see I have a cnt of children = 217.

"myIDstring" is cut-and-pasted from the id attribute of one of the nodes I drilled down on in the javascript console of the console.log(t) echo.

console.log(n); only echos 'false'.

If I try t.find() I get find() is not a function error in the console.

van n = $('#myTree').jstree(true).get_node("myIDstring"); fails just the same as var n = t.get_node();

Thanks.

PS: Yes. I know I could just as well us the value of dta. But my goal is to not use this code from inside the .no('loaded.jstree')

Upvotes: 0

Views: 1652

Answers (1)

Nikolay Ermakov
Nikolay Ermakov

Reputation: 5061

The loaded.jstree is fired when you just have the root node loaded. Better use ready.jstree event like below. Also check demo - Fiddle Demo

$('#myTree')
.jstree({
    ...
})
.on('ready.jstree', function(){     
    var t = $('#myTree').jstree(true);
    var n = t.get_node("myIDstring");
    console.log(t);
    console.log(n);
}

Upvotes: 1

Related Questions