My DOM looks like this:
<div class='stuff'> <a href='#'><img src=''></a> <b>hello</b> <a href='#'><img src=''></a> </div>
Sometimes I have a reference on the first image, and sometimes on the second image.
- When I have the first image using $() how do I select the second image?
- When I have the second image using $() how to I select the 1st image?
.parent().nextAll('a').children('img').parent().prevAll('a').children('img')This is a fairly inelegant way to accomplish it, but it’s quite explicit as to what you’re looking for – almost like XPath. Once you start dealing with more than two links, you may find a technique such as those described by Ben Alpert or cletus to be more flexible (less dependent on a strict hierarchy).