Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traversing up the DOM tree to display info about the parentNode

Tags:

javascript

I feel this should be an easy problem, but alas I find myself stuck. What I'm trying to do is set up a way so that every time you click on a cell on a table(or anywhere), it displays the parentNode of each element, essentially traversing up the DOM tree. I figured I would need to use elem.parentNode, but I'm stuck on the traversing part. Any gurus out there that can help me out, it would be greatly appreciated.

like image 233
NinjaCode Avatar asked Jan 31 '11 18:01

NinjaCode


1 Answers

var element; //your clicked element
while(element.parentNode) {
    //display, log or do what you want with element
    element = element.parentNode;
}
like image 91
Radek Benkel Avatar answered Nov 12 '22 12:11

Radek Benkel