jQuery Element at index selector

Description and Syntax

$(':eq(index)')

selects the element at index n within the matched set.

  • $('li:eq(2)') selects the third <li> element
  • $('.myclass:eq(1)') selects the second element with the class myclass

Examples

The following code selected the third table cells.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--   w w w  . j  ava 2s .  com-->
            $("td:eq(2)").css("color", "red");
        });
    </script>
  </head>
  <body>
    <body>
      <table border="1">
          <tr><td>TD #0</td><td>TD #1</td><td>TD #2</td></tr>
          <tr><td>TD #3</td><td>TD #4</td><td>TD #5</td></tr>
          <tr><td>TD #6</td><td>TD #7</td><td>TD #8</td></tr>
      </table>
    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities