Faced a problem, I can not add a new element of the tree jstree. Tell experts how to add new parent node correctly using a button handler. Here is an example:

$("#CreateButton").button().click(function (){ var ref = $("treeview").jstree("create_node", null, null, "last", function (node) { alert(ref); this.edit(node); }); }); 

    1 answer 1

    In the above code, at least two errors: in $("treeview") forgot # or . , the variable ref is not yet assigned a value at the time the alert(ref) called.

    Working example (replaced alert on console.log )

     $("#treeview").jstree({ "core": { "animation": 0, "check_callback": true, "themes": { "stripes": true } } }); $("#CreateButton").button().click(function() { var ref = $("#treeview").jstree("create_node", null, 'New node', "last", function(node) { console.log(node.text); this.edit(node); }); }); 
     <link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.1/themes/default/style.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.1/jstree.min.js"></script> <button id="CreateButton">New</button> <div id="treeview"></div> 

    • Thank you very much! - stankevich 1:01 pm