With Jquery, I need to select just the first "n" items from the page, for example the first 20 links instead of selecting all of them with the usual
$("a")
Sounds simple but the jQuery manual has no evidence of something like this.
With Jquery, I need to select just the first "n" items from the page, for example the first 20 links instead of selecting all of them with the usual
$("a")
Sounds simple but the jQuery manual has no evidence of something like this.
I found this note in the end of the lt() docs:
So use
$("selector").slice(from, to)
for better performances..slice() isn't always better. In my case, with jQuery 1.7 in Chrome 36, .slice(0, 20) failed with error:
RangeError: Maximum call stack size exceeded
I found that :lt(20) worked without error in this case. I had probably tens of thousands of matching elements.
Use lt pseudo selector:
This matches the elements before the nth one (the nth element excluded). Numbering starts from 0.
JQuery Documentation
Try the :lt selector: http://docs.jquery.com/Selectors/lt#index
You probably want to read up on slice. Your code will look something like this: