jQuery odd() Method

The jQuery odd() method is used to select the odd indexed elements from the selected elements.

Syntax:

$(selector).odd()

Return Value: It returns the odd indexed elements from the selected elements.

Example 1: This example selects the odd indexed list elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        jQuery odd() method
    </title>
  
    <script src=
    </script>
      
    <style>
        li {
            width: 150px;
            margin: 0 auto;
        }
  
        .highlight {
            background-color: green;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
      
    <p id="GFG_UP"></p>
      
    <ul>
        <li>GFG_0</li>
        <li>GFG_1</li>
        <li>GFG_2</li>
        <li>GFG_3</li>
        <li>GFG_4</li>
    </ul>
    <br>
      
    <button onclick="Geeks()">
        Click here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var elUp = document.getElementById("GFG_UP");
        var elDown = document.getElementById("GFG_DOWN");
        elUp.innerHTML = "JQuery | odd() method";
          
        function Geeks() {
            $("ul li").odd().addClass("highlight");
            elDown.innerHTML = 
                "The odd elements are highlighted";
        
    </script>
</body>
  
</html>  

chevron_right


Output:

Example 2: This example checked the odd indexed checkboxes.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        jQuery odd() method
    </title>
  
    <script src=
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
      
    <p id="GFG_UP"></p>
      
    GFG_0: <input type="checkbox" 
        id="GFG_0" value="GFG_0"><br>
  
    GFG_1: <input type="checkbox" 
        id="GFG_1" value="GFG_1"><br>
  
    GFG_2: <input type="checkbox" 
        id="GFG_2" value="GFG_2"><br>
  
    GFG_3: <input type="checkbox" 
        id="GFG_3" value="GFG_3"><br>
  
    GFG_4: <input type="checkbox" 
        id="GFG_4" value="GFG_4">
    <br><br>
      
    <button onclick="Geeks()">
        Click here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var elUp = document.getElementById("GFG_UP");
        var elDown = document.getElementById("GFG_DOWN");
        elUp.innerHTML = "JQuery | odd() method";
          
        function Geeks() {
            $("input").odd().attr("checked", true);
            el_down.innerHTML = 
                "The odd elements are checked";
        
    </script>
</body>
  
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.