In jQuery, you can call closestto find the closest parent.
If I have ain liin ulin tdin table. I want to find out if a parent is closer ulthan a parent table. Obviously, in this case the answer is obvious.
If I run $('a').closest('table').lengthor $('a').closest('ul').length, return 1.
I would like to know which parent is closer in the DOM.
Technically, if there was a method that in this case $('a').closer('ul', 'table')would return ul, because it is closer to the DOM.
<table>
<tr>
<td>
<ul>
<li>
<a href="#">A button</a>
</li>
</ul>
</td>
</tr>
</table>
source
share