Open In App

jQuery slice() Method

Last Updated : 11 Jul, 2023
Like Article

The slice() is an inbuilt method in jQuery that is used to select a subset of elements based on its index. The subset is a set that may be a part of a large set. 


$(selector).slice(para1, para2)

Parameter: It accepts two parameters which are specified below-

  • para1: It specifies where to start the selection of the elements.
  • para2: It is optional and it specifies where to stop the selection of the elements.

Return Value: It returns the subset of the selected element. 

jQuery code to show the working of slice() method: 

Example: In this example, we are using the above-explained method.


<!DOCTYPE html>
    <script src=
    <!-- jQuery code to show the working of this method -->
        $(document).ready(function () {
            $("p").slice(1, 4).css("background-color", "lightgreen");
        body {
            width: 500px;
            height: 200px;
            padding: 20px;
            border: 2px solid green;
    <h1>Welcome to GeeksforGeeks !</h1>
    <p>This is at index 0.</p>
    <p>This is at index 1.</p>
    <p>This is at index 2.</p>
    <p>This is at index 3.</p>

Output: In this code, all paragraph elements from index 1 to 3 get highlighted.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads