You can bake that logic into the selector by combining multiple selectors. For instance, we could target all elements with a given id, that also have a particular class:
This should look similar to something you'd write in CSS. Note that it won't apply to all #foo elements (though there should only be one), and it won't apply to all .bar elements (though there may be many). It will only reference elements that qualify on both attributes.
jQuery also has a great .is method that lets your determine whether an element has certain qualities. You can test a jQuery collection against a string selector, an HTML Element, or another jQuery object. In this case, we'll just check it against a string selector:
$(".bar:first").is("#foo"); // TRUE if first '.bar' in document is also '#foo'
Just to say I eventually solved this using index().
NOTHING else seemed to work.
So for sibling elements this is a good work around if you are first selecting by a common class and then want to modify something differently for each specific one.
EDIT: for those who don't know (like me) index() gives an index value for each element that matches the selector, counting from 0, depending on their order in the DOM. As long as you know how many elements there are with class="foo" you don't need an id.
Obviously this won't always help, but someone might find it useful.