jQuery Odd element selector

Description and Syntax

$(':odd')

selects all elements with an odd index within the matched set.

:odd selects the second element, fourth element, and so on within the matched set.

Examples

  • $('li:odd') selects the odd-indexed elements within the set of <li> elements
  • $('.myclass:odd') selects the odd-indexed elements within the set of elements that have the class myclass

The following code select the second, fourth rows.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from w w w.  j av  a2  s .c  om-->
            $("tr:odd").css("background-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