jQuery Selector How to - Select odd elements, zero-indexed








Question

We would like to know how to select odd elements, zero-indexed.

Answer


<!--   ww  w . j a  v a2s  .  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("background-color", "#bbbbff");
        });
    </script>
  </head>
  <body>
    <table>
        <tr><td>First</td></tr>
        <tr><td>Middle</td></tr>
        <tr><td>Last</td></tr>
    </table>
    </body>
</html>

The code above is rendered as follows: