The :eq()
selector selects an element by index number.
The index numbers start at 0.
$(":eq(index)")
Parameter | Optional | Description |
---|---|---|
index | Required. | Specifies the index of the element |
Select the second <p> element:
<!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"); });/*from ww w. j a v a 2s .c o m*/ </script> </head> <body> <h1>Welcome to My Homepage</h1> <p class="intro">This is a test.</p> <p>I am from java2s.com.</p> <p>This is another test.</p> <p>Who is your favourite:</p> <ul id="choose"> <li>Javascript</li> <li>HTML</li> <li>CSS</li> </ul> </body> </html>