English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery slice() Method

jQuery Traversal Methods

The slice() method selects a subset of matching elements based on their indices.

A subset is a collection that may be part of a larger collection.

Syntax:

$(selector).slice(start, end)

Example

From index number2Start selecting <p> elements from the <p> element:

$("document").ready(function(){
  $("p").slice(2).css("background", "coral");
});
Test to see‹/›

Select <p> elements from start to end using two parameters:

$("document").ready(function(){
  $("p").slice(2, 4).css("background", "coral");
});
Test to see‹/›

Use negative numbers to start selecting <p> elements instead of counting from the beginning:

$("document").ready(function(){
  $("p").slice(-4).css("background", "coral");
});
Test to see‹/›

Parameter Value

ParameterDescription
startSpecify where to start the subset. The first element is zero. You can start from the end of the selection by using negative numbers
endSpecify where to end the subset (excluding the closing element). If not specified, the subset ends at the selection end.

jQuery Traversal Methods