Skip to content
Related Articles

Related Articles

jQuery :nth-child() Selector

Improve Article
Save Article
  • Last Updated : 14 Nov, 2022
Improve Article
Save Article

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

Syntax:

$("Selector:nth-child( index | even |odd |equation )")
  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: 

HTML




<!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
Related Articles

Start Your Coding Journey Now!