Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

jQuery :nth-child() Selector

❮ jQuery Selectors

Example

Select each <p> element that is the third child of its parent:

$("p:nth-child(3)")
Try it Yourself »

Definition and Usage

The :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent.

Tip: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.


Syntax

:nth-child(n|even|odd|formula)

Parameter Description
n The index of each child to match.

Must be a number. The first element has the index number 1.
even Selects each even child element
odd Selects each odd child element
formula Specifies which child element(s) to be selected with a formula (an + b).
Example: p:nth-child(3n+2) selects each 3rd paragraph, starting at the 2nd child element

Try it Yourself - Example

Select each <p> element that is the second child of all <div> elements
How to select each <p> element that is the second child of all <div> elements.

Using a formula (an + b)
How to use a formula (an + b) to select different child elements.

Using "even" and "odd"
How to use even and odd to select different child elements.

Difference between :nth-child(), :nth-last-child(), :nth-of-type() and :nth-of-last-type()
The difference between p:nth-child(2), p:nth-last-child(2), p:nth-of-type(2) and p:nth-of-last-type(2).


❮ jQuery Selectors

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.