jQuery Table How to - Select odd numbered table rows








Question

We would like to know how to select odd numbered table rows.

Answer


<!--from  w  w  w  .  j av a 2 s.  com-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
      $(document).ready(function(){
          $("tr:odd").css("color", "red");
      });
    </script>
  </head>
  <body>
       <table border="1">
        <tr><td>Row</td></tr>
        <tr><td>Row</td></tr>
        <tr><td>Row</td></tr>
        <tr><td>Row</td></tr>
      </table>
  </body>
</html>

The code above is rendered as follows: