slice() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:slice

Description

The slice() method selects a subset of elements based on its index.

Syntax

Parameter Require Description
start Required. start index. The index numbers start at 0.
stop Optional. end index. If omitted, the selection continues until the end of the set.

The index numbers start at 0.

Using a negative number will select elements from the end of the selected elements.

The following code shows how to Start the selection of <p> elements from the <p> element with index number 2:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").slice(1,3).css("color", "red");
});/*from ww w.j ava2  s  .  c o  m*/
</script>
</head>
<body>

<p>this is a test (index 0).</p>
<p>this is a test (index 1).</p>
<p>this is a test (index 2).</p>
<p>this is a test (index 3).</p>

</body>
</html>

Related Tutorials