How do I get the text of the currently selected node in jstree?

12,070

If you are using the jstree with the checkboxes on you can check for the length of the checkbox selectors.

if ($('.jstree-checked').length == 1) {
    alert( 'Checked Item -: ' + $('.jstree-checked').text() );
}

If you are not into checkboxes then use this selector.

if ($('.jstree-clicked').length == 1) {
    alert( 'Selected Item -: ' + $('.jstree-clicked').text() );
}

To get the list of all the items and methods for the jstree , you need to put a debug point in either chrome or Firebug in the jstree.js file .. For example you can find the .bind method in the .js and put a debug point on that particular line.. When that point hits you can hover over the arguments to get a glimpse of the methods in it..

Share:
12,070
TheDude
Author by

TheDude

Updated on June 04, 2022

Comments

  • TheDude
    TheDude almost 2 years

    This seems like it should be simple to do, but the jstree "data.rslt.obj.text()" method likes to return the text of the current node as well as all of its children. I've figured out how to get the current node name with renaming a node (new_name), but how do I get the text of ONLY the current node when I simply select it? Also, I'd appreciate any insight into how I would find all of these methods and properties in the jstree using chrome or firebug. Where would I look in the list of elements that come up when I select "data.rslt", for instance. Thanks!

     $("#RequirementsTree")
        .bind("select_node.jstree", function(event, data) {
                if(is_requirement_node(data))
                {
                    var ReqCheck = data.rslt.obj.attr("name");
    
                    @* This is a REQUIREMENT *@
                    if(ReqCheck == "requirement")
                    {
                        $("#RMSDoc_RequirementFlag").val("EDIT");
                        $("#RMSDoc_RBSRequirement_RequirementsId").val(data.rslt.obj.attr("id").substring(4));
                        $("#RMSDoc.RBSRequirement.RequirementsId").val(data.rslt.obj.attr("id").substring(4));
                        $("#RMSDoc_RBSRequirement_RequirementsText").val($.trim(data.rslt.obj.text()));
                        $("#RMSDoc_TreeBranch_Text").val("");
                        $("#HierarchyText").hide();
                        $("#RMSDoc_TreeBranch_Text").hide();
                        $("#ExistingTreeSubmit").val("@Model.RMSDoc.RMSEditReqButton.ConfigurableLabelDesc");
    
                    }
                    else {
                        alert("Requirement node select error");
                    }
                }
                @* This is a TREE BRANCH *@
                else
                {
                    debugger;
                    $("#RMSDoc_RequirementFlag").val("ADD");
                    $("#HierarchyText").show();
                    $("#RMSDoc_TreeBranch_Text").show();
                    $("#RMSDoc_TreeBranch_Text").val($.trim(data.rslt.obj.text()));
                    $("#RMSDoc_TreeBranch_id").val(data.rslt.obj.attr("id").substring(4));
                    $("#RMSDoc_RBSRequirement_RequirementsText").val("");
                    $("#ExistingTreeSubmit").val("@Model.RMSDoc.RMSCreateReqButton.ConfigurableLabelDesc");
                }
         })
        .bind("create.jstree", function(e, data) {
    
  • TheDude
    TheDude over 11 years
    Thanks! That worked. $('.jstree-clicked').text() is far better than using data.rslt.obj.text() since it only grabs the text of the node you're clicking on instead of including all of the children.