:eq() Selector - Javascript jQuery Selector

Javascript examples for jQuery Selector:eq

Description

The :eq() selector selects an element by index number, which start at 0.

Syntax

Parameter Description
index Required. index of the element, first element is 0

The following code shows how to select the second <p> element:

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:eq(1)").css("background-color","yellow");
});/*from   w w w  .  j a  v  a  2 s  . co  m*/
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p class="intro">This is a test.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>



<p>Who is your favourite:</p>
<ul id="choose">
  <li>A</li>
  <li>B</li>
  <li>C</li>
</ul>

</body>
</html>

Related Tutorials