Reputation: 18127
How can an ancestor (a "parent" element some levels above) of a clicked element be found?
The element is not a direct parent but it is further up in the DOM tree.
In the example below i need to find <a id='collapser1'>
if <a id='collapser4'>
is clicked. How to do that?
<ul id="pdfbutton">
<li>
<a id="collapser1" href="#collapser1"
class="jqcNode" style="cursor: pointer;">Link 1
</a>
<ul style="display: block;">
<li>
<a id="collapser2" href="#collapser2"
class="jqcNode" style="cursor: pointer;">Link 2
</a>
<ul style="display: none;">
<li>Text</li>
</ul>
</li>
<li>
<a id="collapser3" href="#collapser3"
class="jqcNode" style="cursor: pointer;">Link 3
</a>
<ul style="display: none;">
<li>Text</li>
</ul>
</li>
<li>
<a id="collapser4" href="#collapser4"
class="jqcNode" style="cursor: pointer;">Link 4
</a>
<ul style="display: none;">
<li>Text</li>
</ul>
</li>
</ul>
</li>
</ul>
Upvotes: 2
Views: 1885
Reputation: 700830
Navigate to the ul
ancestor, then to it's li
ancestor, then into the a
tag:
$(this).closest('ul').closest('li').children('a')
Upvotes: 4
Reputation: 3828
try this.
$(element).parent() returns the immediate parent.
$("#collapser3").click(function() {
var parentElement = $(this).parent();
});
this will may helpful to you.
Thanks.
Upvotes: -2