20

I am not talking about $("#demo1").jstree("rename",node) which makes the node editable for the user. I am talking about the name being changed within the code. For example my nodes are all prefixed with a 2 digit number "[01]" so before I call $("#demo1").jstree("rename",node) I want to strip out the prefix, and put it back in once the user has finished editing. I have tried selecting "#nodeid a" but inside the hyperlink there is an ins tag and this gets replaced if i replace the URL contents. The documentation hasn't been helpful and I havent had much luck looking through the libraries code, can any help me? Chris

Chris
  • 2,340
  • 6
  • 40
  • 63

4 Answers4

29

The recommended method is to use rename_node

$("#demo1").jstree('rename_node', node , text );

Please keep in mind that by default all modifications to the tree are prevented (create, rename, move, delete). To enable them set core.check_callback to true

$('#demo1').jstree({
    'core': {
        'check_callback': true,
        /// rest of the options...
    }
});

Rename your node (alternative, not recommended)

$("#demo1").jstree('set_text', node , text );

Debugging

If you still encounter trouble, you can use this method to get the last error.

$('#demo1').jstree(true).last_error()

For older versions (v1.*)

$("#demo1").jstree('rename_node', [node , text] ); 
$("#demo1").jstree('set_text', [node , text] ); 

See also:

Arend
  • 3,741
  • 2
  • 27
  • 37
  • 2
    According to [the doc](https://www.jstree.com/api/#/?f=set_text(obj%2C%20val)), `set_text()` is **used internally, please use `rename_node(obj, val)`**. – Pang Oct 13 '15 at 04:58
  • As mentioned by @jnoreiga : for 'set_text' method I have to change `rename_node` syntax to '$("#demo1").jstree('rename_node', [node ], text );` then it worked. – Twix Jan 02 '16 at 06:24
  • 1
    dom, You're right. The answer still reflected the solution at the time of the answer (for version 1.*). just updated this example to the recent versions of jsTree (v3.*), whch has some additional requirements regarding check_callback. – Arend Sep 03 '16 at 21:47
  • @Arend Thanks a ton..updated ans help me lot and finally check_callback work for me thanks again... – dom Sep 04 '16 at 07:34
  • 1
    You can also use `id` string value instead of `node` object for first parameter. (I checked on v3.3.5) – wonsuc Jul 30 '18 at 06:15
5

I believe there is an syntax error with respect to the square braces "[" in the above answer. I use jsTree 3.0.4 and this is the correct syntax -

right -    $("#demo1").jstree('set_text',node,text);
wrong -    $("#demo1").jstree('rename_node', [node , text] );    

Example - 
$("#tree_3").jstree('set_text',"#idSelectorForNode" ,"NewName");
  • Thanks!! better - $("#demo1").jstree('rename_node',node,text); ( because set_text is private method) – Shoham Mar 01 '16 at 10:54
1

You should turn on the switch to allow the rename operation, such as:

$('#container').jstree({
    'core' : {
        'check_callback' : function (operation, node, node_parent, node_position, more) {
            // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
            // in case of 'rename_node' node_position is filled with the new node name
            return operation === 'rename_node' ? true : false;
        }

});
Cherry
  • 11
  • 2
0

You can use this for updating node text with jstree refresh:

$("#demo1").jstree(true).rename_node(node , "Renamed_Text");
Aziza Kasenova
  • 1,501
  • 2
  • 10
  • 22
  • This answer is very similar to [the most well received answer](https://stackoverflow.com/a/6256060/14267427) to this question. – Tyler2P Aug 02 '21 at 16:29