jQuery :nth-child Selector

jQuery | :nth-child Selector: Learn about the jQuery :nth-child Selector with its usages, syntax, and examples.
Submitted by Pratishtha Saxena, on November 04, 2022

:nth-child Selector

In jQuery, selectors are a parameter through which we can select the HTML element. Selectors can comprise – class names, ids, tag names, attributes, etc. Basically, by using selectors, the appropriate HTML-DOM element can be selected and hence performed actions on it. Here, we are discussing the jQuery :nth-child Selector.

The jQuery :nth-child selector helps to select the nth child occurrence of the specified tag in their parent elements. It is used along with some tag names prepended to it. It will return the nth child of the parent element if the nth child is of the specified tag.

:nth-child Selector Syntax


It takes a few parameters: n, even, odd, formula.

  • n : is the index of the child element that you want to select.
  • even : the keyword 'even' can be passed to get the child elements at the even index position.
  • odd : the keyword 'odd' can be passed to get the child elements at the odd index position.
  • formula : Any specific formula or equation can be passed in order to get that particular indexed element.

Once the elements are selected, you can access them and perform the actions that you want to perform on them. The example given below shows the selection of the nth child element of 'div' tag on click by passing the appropriate selector.

jQuery :nth-child Selector Example

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <h2>jQuery - Nth Child Selector</h2>
    <p>Click the button and get the nth child selector of the selected element.</p>
    <button>Nth Child Selector</button>
      <div>Div One</div>
      <div>Div Two</div>
      <div>Div Three</div>
      <div>Div Four</div>
      <div>Div Five</div>
      <div>Div Six</div>
      <div>Div Seven</div>
  <script type="text/javascript">
            $('div:nth-child(3)').css({'color':'red', 'font-size':'larger'});


Example 1: jQuery :nth-child Selector

Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.