Assume an html document:
<html>
<head>
</head>
<body>
<li id="eee">
<li>aaa</li>
<span>bbb</span>
...
<div>
<button id="ccc">ddd</button>
</div>
...
</li>
</body>
</html>
Using $('#ccc').closest('li')
jQuery will select the <li id="eee">...</li>
- the nearest ancestor of the <button id="ccc">
. Is it possible to select <li>aaa</li>
as "really" closest <li>
element to the <button id="ccc">
(so, it need not be an ancestor, but enough be a sibling of one of the ancestors)?
valid html (specially for @PSL guy, already deleted his comments):
<html>
<head>
</head>
<body>
<div id="eee">
<div>aaa</div>
<span>bbb</span>
<ul>
<li>fff</li>
<li>
<button id="ccc">ddd</button>
</li>
</ul>
</li>
</body>
</html>
how to get the <div>aaa</div>
without using the parent()
function, but using smth. like trueClosest('div')
taking siblings, ancestors and siblings of the ancestors in account?
I just want to know, if jQuery has such a function or not?
One could use the following:
$('#startingElement').closest(':has(#desiredElement)').children('#desiredElement')
So: find the closest element to the #startingElement
which has a #desiredElement
and then get it(them) through children(function);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With