Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery limit element query results [duplicate]

Possible Duplicate:
Selecting the first “n” items with jQuery

I have an infinite countable list of elements!

Is there a function to find x elements from the head of the list and stop!

$('.elements').find('li').limit(10) //this does not work

I tried the goog but i could not be specific enough with the ? !

like image 324
James Andino Avatar asked Dec 22 '12 19:12

James Andino


2 Answers

Try jQuery slice. Your code will be something like this:

$('.elements').find('li').slice(0,10);

UPDATE

As suggested by Marius Miliunas and T.J. Crowder, based on performance, you can just do this:

$('.elements li').slice(0,10);
like image 94
palaѕн Avatar answered Nov 14 '22 06:11

palaѕн


Yes, you can use jQuery's custom :lt selector for that.

For instance, this will find only the first three lis in the given target list:

var firstThree = $("#target li:lt(3)");

Live Example | Source

like image 37
T.J. Crowder Avatar answered Nov 14 '22 06:11

T.J. Crowder