JavaScript
Article

jquery select first x number of elements

By Sam Deering

Quick jQuery snippet to select first x number of elements. You can also use the jQuery .slice() function which can chop up element groups into a range for you with a combination of .get();

//get first 20 anchor tags
$("a").slice(0,20)

jQuery slice function example demo

Also you can use lt pseudo selector: This matches the elements before the nth one (the nth element excluded). Numbering starts from 0.

$(“a:lt(n)”)

//instead of this:

for (i=0; i

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.