I can’t find a selector suited for my needs, that is get the first (and only) .info span element before img element. Please be patient, i’m new to jQuery:
<div> <!-- common parent -->
<span class="info"></span>
<p>Stuff</p><span>Other sfuff</span>
<!-- p, span or possibly other dom element before img -->
<img src="" alt="" class="highlight" />
<span class="info"></span>
<!-- here nothing precede img element -->
<img src="" alt="" class="highlight" />
</div>
I’ve tried:
$("img.highlight").prev("span.info") (that is immediately preceding sibling) will fail in the first example while works in the second one.
$("img.highlight").prevAll("span.info") will fail with the second one because the first span will be selected too.
EDIT: to be more clean this is real code:
$("img.highlight").each(function() {
$info = $(this).selector("span.info") // selector() to be replaced
$info.text($(this).attr("src")); // Fill the right span.info
});
$('img.highlight').prevUntil('img.highlight', 'span.info')