- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
jQuery :nth-child() Selector
The :nth-child() selector in jQuery is used to select all elements that are the nth child, regardless of type, of their parent.
Syntax
The syntax is as follows −
:nth-child(n|even|odd|formula)
Above, the n parameter is the index of each child to match. The even parameter selects even child element, whereas odd selects odd child element.
Example
Let us now see an example to implement the jQuery :nth-child() selector −
<!DOCTYPE html> <html> <head> <style> .one { color: white; background-color: green; font-size: 16px; border: 2px blue solid; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p:nth-child(2)").addClass("one"); }); </script> </head> <body> <h1>Match Details</h1> <p>Date: 29th October 2019</p> <p>Timings: 3PM TO 8PM</p> <p>Ground: Lords</p> <p> Entry begins 2PM</p> </body> </html>
Output
This will produce the following output −
- Related Articles
- jQuery :nth-last-child() Selector
- jQuery :first-child Selector
- jQuery :last-child Selector
- jQuery :only-child Selector
- jQuery parent > child Selector
- jQuery :nth-of-type() Selector
- Role of CSS :nth-child(n) Selector
- jQuery :nth-last-of-type() Selector
- Role of CSS :nth-last-child(n) Selector
- CSS Child Selector
- jQuery :button Selector
- jQuery :checkbox Selector
- jQuery :checked Selector
- jQuery :contains() Selector
- jQuery :disabled Selector

Advertisements