Home >>jQuery Tutorial >jQuery slice() Method

jQuery slice() Method

jQuery slice() Method

jQuery slice() method in jQuery is used to selects a subset (a set that is a part of a larger set) of elements based on its index.

Syntax:
$(selector).slice(start,stop)

Parameter Values

Parameter Description
start It is a Required parameter and is used to specifies where to start the selection of elements. The index numbers start at 0.
stop It is an Optional parameter and is used to specifies where to end the selection of elements. The index numbers start at 0.
Here is an Example of jQuery slice() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").slice(2).css("background-color", "#3e88f0");
});
</script>
</head>
<body>
<h1>Welcome to Phptpoint</h1>
<p>My name is phptpoint</p>
<p>This is First paragraph</p>
<p>I live in Noida (index 2)</p>
<p>This is Second paragraph</p>
</body>
</html>

Output:

Welcome to Phptpoint

My name is phptpoint

This is First paragraph

I live in Noida (index 2)

This is Second paragraph


No Sidebar ads