Re: Keep track of parent node
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Keep track of parent node

From: RobG <rgqld@iinet.net.au>
Date: Wed Feb 01 2006 - 07:01:21 CET

impaler wrote:
> rmlakshmanan@gmail.com wrote:
>
>>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
>
>
> var arrayParents = new Array;
> if (child.parent) {
> var currentParent = child.parent;
> arrayParents.push(currentParent);
> while(currentParent.parent) {
> currentParent = currentParent.parent;
> arrayParents.push(currentParent);
> }
> }

More concisely:

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

-- 
Rob
Received on Tue Feb 7 21:29:38 2006