Slicing Up Selector Results

$("tr").slice(2,5);   //selects the <tr> elements between 2 and 4 inclusive $("p").slice(1,-1);   //selects all <p> elments except the first and last

The .slice(start, [end]) method removes elements before index start and after index end and returns only the items in between. This allows you to break large selections into smaller chunks. The indexes are zero based. A negative end indicates the offset from the end of the list, where –1 is the last element and –2 is the second to the last element.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.