jQuery eq()

Introduction

Select the second <p> element whose index number is 1:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").eq(1).css("background-color", "yellow");
});/*  www  . j ava2 s. co m*/
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>
<p>This is a test (index 0).</p>
<p>CSS (index 1).</p>
<p>I am from java2s.com (index 2).</p>
<p>This is another test (index 3).</p>

</body>
</html>

The eq() method returns an element with a specific index number of the selected elements.

The index numbers start at 0.

$(selector).eq(index)
Parameter
Optional
Description
index


Required.


index of the element.
Can either be a positive or negative number.
A negative number will start the index count from the end of the selected elements.



PreviousNext

Related