To get the immediate parent, you can use:
Code:
var pid=this.parentNode.id;
However, if there is no id or no parent node, there will be problems. So, you might want to do:
Code:
if (this.parentNode&&this.parentNode.id)
var pid=this.parentNode.id;
This also makes 'walking' the nodes looking for id's a bit tricky but, it can be done. It would be helpful to know which parent or id you are looking for, why you are looking for it and, what sorts of things could be there when you go looking.
In one scenario, we could be looking for the parent node with a specified id, this would do that:
Code:
function find_pid(el, id){
var p=el;
while(p=p.parentNode;)
if(p.id&&p.id==id)
return p;
return null;
}
You can feed it the child and the id you are looking for. If the child has a parent with the specified id, it will return that parent, otherwise, it will return null.
Bookmarks