Javatpoint Logo
Javatpoint Logo

jQuery slice() method

The jQuery slice() method selects a subset (part of a larger set) of matched elements based on the range of indices. This method limits the elements selection in the group.

It returns the elements between the start index and end index value. The index starts with 0. If we do not specify the end index, the method will start from the start index and will select every matched element until the last matched element is selected.

Syntax

This method accepts two parameters in which the first is mandatory, and another one is optional. The parameter values are defined as follows -

start: It is a mandatory parameter. It is an integer that represents the index value from where the select the elements. The index starts with 0. Negative values are allowed. Using the negative value will begin the selection from the end of matched elements.

stop: It is an optional parameter. It is an integer that represents the index value up to which the elements get selected. If it is omitted, the selection will continue to the end of the set.

Now, let's see some simple examples of using the jQuery slice() method.

Example1

In this example, there are several paragraph elements. The paragraph starts with the index value 0. Here, we are using both parameters of the slice() method with starting value 1 and stop value is 4 to select the paragraphs in the specified range. The method will select the paragraph elements to start from the index value 1, and the selection will continue to the endpoint.

Test it Now

Output

jQuery slice() method

After clicking the given button, the elements in the specified range will get selected as shown in the below screenshot -

jQuery slice() method

Example2

Here, we are using the negative values of both parameters of the slice() method with starting value -4, and stop value is -1 to select the paragraphs in the specified range.

Test it Now

Output

jQuery slice() method

After clicking the given button, the elements in the specified range will get selected as shown in the below screenshot -

jQuery slice() method




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA