Cosmin Cretu Cosmin Cretu - 3 months ago 45
jQuery Question

jsTree fails on second refresh

I'm using a jsTree plugin and found that on the second time I call the refresh(), the tree will remain in an error / loading state.
Here is the code:

.on('changed.jstree', function (e, data) {
var i, j, selectedNode;
selectedNode = data.node;
currentlySelectedNodeGUID =;
//for (i = 0, j = data.selected.length; i < j; i++) {
// selectedNode = data.instance.get_node(data.selected[i]);
// currentlySelectedNodeGUID =;
url: '@Url.Action("LocationProperties","Location")',
type: 'GET',
data: { id: currentlySelectedNodeGUID },
success: function (result) {
error: function (e) {

The only problem I found was that I get this error
Uncaught TypeError: Cannot read property 'id' of undefined

at this line
currentlySelectedNodeGUID =;

The data however is there, the
does contain an id property and it does have a value, which gets assigned to

I'm pretty much clueless as to why this happens. Any help please?


When you call refresh() your currently selected node is deselected, which triggers the changed event. When there is no node selected data.node is null (or undefined). That's why also is undefined. After the tree has been refreshed the node gets selected again, which triggers changed once again - this time with the node object available.

So you'll need to check if there actually is a node selected before you send that ajax query.