March 12, 2012

jquery selectors: how to select all li items from some index to the end?

Question by myWallJSON

So we can select one item li:eq('+(lastItemIndex)+')' I wonder how to select this item and all items that are next from it to the end? jsfiddle demo here (in which we would like to change all elements in li from 3 to last one)

Answer by Nicola Peluchetti

You should use nextAll();

var lastItemIndex = 3;
$('li').eq(lastItemIndex).nextAll('li').css("opacity", 0);​

http://jsfiddle.net/LN6ak/2/

Answer by Starx

You can combine :gt and :lt to create yourself a range selector

 $('ul li:gt(2):lt('+$('ul li').length+')')

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!