Keep track of parent node

R

rmlakshmanan

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
 
I

impaler

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);
}
}

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

RobG

impaler said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top