In the scenario of HTML code like this:
<html>
<h1>heading</h1>
<div>
<div>
<span>
<img src="source"/>
</span>
</div>
</div>
</html>
The example above is just a template, applicable to any HTML code.
I am looking to target the next image tag after the <h1>
element, regardless of its position in the hierarchy.
Using methods such as $('h1').nextAll('img').first()
or
$('h1').nextUntil('img').last().next()
won't be effective here since they are not direct siblings of the <h1>
. Is there a more concise way to achieve this without resorting to excessive looping?