I am trying to write a jQuery script that will add a class to list items within a certain ID range. I use numbers in my ID’s and want to adjust a range of ID’s.
<li id="item-15">Something</li>
<li id="item-16">Something</li>
<li id="item-17">Something</li>
<li id="item-18">Something</li>
<li id="item-19">Something</li>
I want to add a class to say items 16 through 19. How would I do this?
jQuery('li#item-[16-19]).addClass('the-class');
I am not really sure how to do it. Maybe .each()?
this is what the jquery .slice()
method was designed for.
so
live example:
http://jsfiddle.net/VpNnJ/
you could also combine the :gt() and :lt() selectors as follows
$('li:gt(16):lt(19)').addClass('the-class');again a live example: http://jsfiddle.net/cLjXE/