Search code examples
javascriptjqueryfancytree

Get selected node's parent in FancyTree


I'm working on FancyTree.js for my current project. I need a simple requirement in the Tree.

My requirement is I need to get current selected node's parent node. I'm searching everything, but I can't find the solution.

This link about FancyTreeApi has many options. But none of those are related to my requirement.

EDITED

I have done to get the current selected node using the below code.

 var node = $("#tree").fancytree("getActiveNode");
  if( node ){
    alert("Currently active: " + node.title);
  }else{
    alert("No active node.");
  }

Solution

  • I have researched your question and you can do it in some ways:

    First:

      var node = $("#tree").fancytree("getActiveNode");
      if( node ){
        console.log("Parent of FancytreeNode type: ");
        console.dir(node.parent);
      }else{
        console.log("No active node.");
      }
    

    where node.parent is parent tree node

    Second:

      var node = $("#tree").fancytree("getActiveNode");
      if( node ){
        console.log("Parent of HTMLElement type: ");
        console.dir(node.li.parentNode);
      }else{
        console.log("No active node.");
      }
    

    where node.li is HTML element of active tree node and node.li.parentNode is it's parent HTML element