If I have a simple HTML list
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li id="some-id">Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
it's easy to select every list item after #some-id
:
$("#some-id ~ li")
but how do I select the items before #some-id
?
Use .prevAll()
, like this:
$("#some-id").prevAll()
For example:
$("#some-id").prevAll().css('color', 'red');
Give it a try here, there's not a "previous siblings" selector like your next-siblings selector, but .prevAll()
will get the elements you want, the same as you could substitute your current selector with $("#some-id").nextAll()
.
$("#some-id").prevAll()
See the documentation: http://api.jquery.com/prevAll/