Keep track of parent node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rmlakshmanan@gmail.com

    Keep track of parent node

    In javascript a tree structure node i have a subnode and it has
    parentnode and that parentnode has one parent how do i get all parent
    node when i click the subnode

  • impaler

    #2
    Re: Keep track of parent node


    rmlakshmanan@gm ail.com wrote:[color=blue]
    > In javascript a tree structure node i have a subnode and it has
    > parentnode and that parentnode has one parent how do i get all parent
    > node when i click the subnode[/color]

    var arrayParents = new Array;
    if (child.parent) {
    var currentParent = child.parent;
    arrayParents.pu sh(currentParen t);
    while(currentPa rent.parent) {
    currentParent = currentParent.p arent;
    arrayParents.pu sh(currentParen t);
    }
    }

    and there you have the array of parents. Adapt this algorithm to your
    tree and code

    Comment

    • rmlakshmanan@gmail.com

      #3
      Re: Keep track of parent node

      Thanks a lot

      Comment

      • RobG

        #4
        Re: Keep track of parent node

        impaler wrote:[color=blue]
        > rmlakshmanan@gm ail.com wrote:
        >[color=green]
        >>In javascript a tree structure node i have a subnode and it has
        >>parentnode and that parentnode has one parent how do i get all parent
        >>node when i click the subnode[/color]
        >
        >
        > var arrayParents = new Array;
        > if (child.parent) {
        > var currentParent = child.parent;
        > arrayParents.pu sh(currentParen t);
        > while(currentPa rent.parent) {
        > currentParent = currentParent.p arent;
        > arrayParents.pu sh(currentParen t);
        > }
        > }[/color]

        More concisely:

        function getParentArray( el)
        {
        var parent, parentArray = [];
        while((el = el.parentNode)) {
        parentArray.pus h(el);
        }
        return parentArray;
        }




        --
        Rob

        Comment

        • RobG

          #5
          Re: Keep track of parent node

          RobG wrote:
          [...][color=blue]
          >
          > function getParentArray( el)
          > {
          > var parent, parentArray = [];[/color]

          Aggh...

          var parentArray = [];

          [color=blue]
          > while((el = el.parentNode)) {
          > parentArray.pus h(el);
          > }
          > return parentArray;
          > }[/color]


          --
          Rob

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Keep track of parent node

            RobG wrote:
            [color=blue]
            > RobG wrote:
            > [...][color=green]
            >> function getParentArray( el)
            >> {
            >> var parent, parentArray = [];[/color]
            >
            > Aggh...
            >
            > var parentArray = [];[/color]

            That is equivalent :)


            Regards,
            PointedEars

            Comment

            Working...