[jQuery] get parent where
I've been trying to get a specific parent element traversing up the
DOM until I find it, but it is getting to be some very verbose code,
and I'm thinking there must be a better way.
I have a bunch of events which is fired on a click, and I am trying to
figure out which of the parent divs was clicked.
Now I have something like this (this is a simplified version)
<code>
<div id="firstArea">
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
</ul>
</div>
<div id="secondArea">
<span id="subSecond">
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
</ul>
</span>
</div>
<div id="thirdArea">
<span id="subSecond">
<ul>
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
</ul>
</span>
</div>
</code>
So, i'm trying to get the id of the div.
I was hoping I could do this
<code>
$(this).parent('div').attr(id);
</code>
but unfortunately that doesn't work as the first parent is not always
a div.
So what I end up with is
<code>
var getParent=$(this).parent().attr(id);
if(getParent=='subSecond'){
getParent=$(this).parent().parent().attr(id);
}
</code>
Not the worst code ever, but I think there must be a better way to do
this, and it may get more confusing/verbose as I continue to add
elements and types.
Is there a better way of doing this?