21

Could you please give me the list of parameters of this function and an example of usage

$('#treepanel').jstree("create_node");
Artur Keyan
  • 7,643
  • 12
  • 52
  • 65

2 Answers2

25

IMHO jsTree is powerful, but documentation could be improved.

The create_node function is documented here.

Be careful not interpreting the [] as a literal. They are just to indicate that the parameters are optional.

This works for jsTree version "pre 1.0 fixed":

var position = 'inside';
var parent = $('#your-tree').jstree('get_selected');
var newNode = { state: "open", data: "New nooooode!" };
$('#your-tree').jstree(
    "create_node", parent, position, newNode, false, false);

JSTree 3.3.5

From their docs "create_node" functionality has inverted args 'newNode' and 'position'

 $('#your-tree').jstree("create_node", parent, newNode, position, false, false);

https://www.jstree.com/api/#/?f=create_node([par,%20node,%20pos,%20callback,%20is_loaded])

Nicolò
  • 186
  • 1
  • 16
olafure
  • 3,518
  • 2
  • 33
  • 45
19

More recently, for version 3+:

var parent = '#';
var node = { id:123,text:"Hello world"};
$('#yourtree').jstree('create_node', parent, node, 'last');

Alternative syntax that seems to be working:

$('#yourtree').jstree().create_node(parent, node, 'last');

See documentation

periklis
  • 10,102
  • 6
  • 60
  • 68
  • 11
    if create_node keeps returning false for you, try setting core.check_callback to true. – stefkin Oct 20 '15 at 12:29
  • 2
    @bublik42: Thank you so much, I wasted an hour with jstree's hellishly frustrating documentation before reading your comment! – rvighne Jul 08 '16 at 01:56
  • Is there a way to bind additional data with the newNode? I tried "var node = {id:123, data: {'attr1': 'val1', 'attr2': 'val2'}, text: 'myText'}" But it doesn't work! – Yash P Shah Apr 06 '18 at 22:10