By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

Keep track of parent node

P: n/a
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

Jan 31 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

rm**********@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);
}
}

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

Jan 31 '06 #2

P: n/a
Thanks a lot

Feb 1 '06 #3

P: n/a
impaler wrote:
rm**********@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
Feb 1 '06 #4

P: n/a
RobG wrote:
[...]

function getParentArray(el)
{
var parent, parentArray = [];
Aggh...

var parentArray = [];

while((el = el.parentNode)){
parentArray.push(el);
}
return parentArray;
}

--
Rob
Feb 1 '06 #5

P: n/a
RobG wrote:
RobG wrote:
[...]
function getParentArray(el)
{
var parent, parentArray = [];


Aggh...

var parentArray = [];


That is equivalent :)
Regards,
PointedEars
Feb 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.