I am dynamically creating buttons in javascript, and each button is supposed to select their corresponding node in a jsTree. The problem is that all the buttons select the last node in their respective branch. The jsTree is working as it is supposed to, so that is not the issue. The relevant code:
var children = node.children;
for (var i = 0; i < children.length; i++) {
var childNode = myTree.get_node(children[i]);
var myButton = document.createElement("div");
myButton.className = 'imageButton';
myButton.onclick = function () {
myTree.deselect_all([true]);
myTree.select_node(childNode);
};
When I am building the buttons, the childNode is the correct node.
The problem is when the onClick fires, the childNode is the the last element in children[]. Which is then selected.
Any suggestions?