I have a function that needs to go through alot of parentElement
s to get an id. Instead of writing if( element.parentElement.parentElement.parentElement.id == "target" ){ doThis() }
Is there a way in javascript just to say "If there is a parent element with "this id" doThis()" ? Instead of going through all those parentElements is there a way to just say if there is ANY element that is a parent to the current element with this ID, then doThis()?
EDIT
Many have said i should just getElementById
and maybe that is the solution, but let me show you a example of my code so you can see a little better what I'm doing:
I have alot of divs with the same function. lets call it clicked().
so say i have 20 divs that look like this: <div onclick = "clicked( this )">Content</div>
Lets say these divs are contained within a few other divs:
<div id = "container">
<div id = "top">
<div id = "left">
<div onclick = "clicked( this )">Content</div>
<div onclick = "clicked( this )">Content2</div>
...
</div>
</div>
</div>
my clicked function looks like:
function clicked( element ){
if( element.parentElement.parentElement.parentElement.id == 'container' ){
doThis();
}
}