Search code examples
javascriptjqueryjstree

Checking a checkbox node programmatically with jsTree


In a tree built with jsTree, I have the text within the <a> tag sitting in a variable. I would like to check that node. How can I do so?

I am currently finding that node, using jQuery, and altering its class. However, this does not repair the parent node by making the parent undetermined in its class. I tried doing $('.colors').jstree("checkbox_repair"), but that didn't seem to do anything.

It would be great if someone could actually answer both those questions, since they are related to the same problem.

Here is a jsFiddle, illustrating the issue--> http://jsfiddle.net/thapar/5XAjU/


Solution

  • In js_tree there are .check_node ( node ) and .uncheck_node ( node ) functions, i think this is what you are asking for. Soe the documentation here: http://www.jstree.com/documentation/checkbox

    This is an excerpt from the documentation in the link above, "how to perform an operation":

    /* METHOD ONE */
    jQuery("some-selector-to-container-node-here")
        .jstree("operation_name" [, argument_1, argument_2, ...]);
    
    /* METHOD TWO */
    jQuery.jstree._reference(needle)
        /* NEEDLE can be a DOM node or selector for the container or a node within the container */
        .operation_name([ argument_1, argument_2, ...]);
    

    So I think this syntax should work

    $.jstree._reference(".colors").check_node('li#tree_3');
    

    Also i am not sure you should be using a class to reference your tree. Probably use an ID to reference your tree, and then use this syntax:

    $.jstree._reference("#colors").check_node('li#tree_3');
    

    //EDIT: Please keep in mind that the newest version of jsTree doesn't have a function called _reference anymore. It got renamed to reference (without the leading underscore). (Last checked 24/08/2015 15:45 by @mkli90) Link: https://www.jstree.com/api/#/?f=$.jstree.reference(needle)