How does jQuery.eq() method work in jQuery?


If you want to return an element with a definite index number of the selected elements, then use the eq() method. The first element has index 0, the second element has index 1, etc. Yes, the index number starts at 0.

Example

You can try to run the following code to learn how to work with jQuery.eq() method,

Live Demo

<!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").eq(3).css("background-color", "gray");
  });
</script>
</head>
<body>
 <h1>Tutorialspoint</h1>

 <p>Free Learning</p>
 <p>Free Text Tutorials</p>
 <p>Free Video Tutorials</p>
 <p>Tutor Connect</p>
</body>
</html>

Updated on: 09-Dec-2019

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements