Skip to content
Related Articles

Related Articles

jQuery | :nth-child() Selector
  • Last Updated : 12 Mar, 2019

jQuery :nth-child() Selector Selects all elements that are the nth-child of their parent.

Syntax:

$("Element:nth-child(Index/even/odd/equation)")
    Values:

  1. Index: Index provided. Index starts from
  2. even: even number of child elements get selected.
  3. odd: odd number of child elements will get selected.
  4. equation: select child elements by solving a formula(an+b).

Example:




<!DOCTYPE html>
<html>
  
<head>
    <script src=
  </script>
    
    <script>
      $(document).ready(function() {
         $("p:nth-child(2n-1)").css("background-color", 
                                    "lightgreen");
        });
    </script>
</head>
  
<body>
  
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
    <p>GFG</p>
  
</body>
  
</html>


Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :