Open In App

jQuery :nth-child() Selector

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 )")

Example: In this example, we are using the above-explained nth-child() selector.




<!DOCTYPE html>
<html>
 
<head>
    <title>jQuery :nth-child() Selector</title>
    <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:



 

Article Tags :