jQuery Selector How to - Select a single element by its index








Question

We would like to know how to select a single element by its index.

Answer


<!--   w  ww  .  ja v  a2s. c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("td:eq(2)").css("color", "red");                
        });
    </script>
  </head>
  <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>

The code above is rendered as follows: