jQuery API

:nth-child() Selector

nth-child selector

version added: 1.1.4jQuery(':nth-child(index/even/odd/equation)')

  • index
    The index of each child to match, starting with 1, the string even or odd, or an equation ( eg. :nth-child(even), :nth-child(4n) )

Description: Selects all elements that are the nth-child of their parent.

Because jQuery's implementation of :nth-child(n) is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1. For all other selector expressions, however, jQuery follows JavaScript's "0-indexed" counting. Therefore, given a single <ul> containing two <li>s, $('li:nth-child(1)') selects the first <li> while $('li:eq(1)') selects the second.

The :nth-child(n) pseudo-class is easily confused with :eq(n), even though the two can result in dramatically different matched elements. With :nth-child(n), all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class. With :eq(n) only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the (n+1)th one (n is 0-based) is selected.

Further discussion of this unusual usage can be found in the W3C CSS specification.

Examples:

Example: Finds the second li in each matched ul and notes it.

<!DOCTYPE html>
<html>
<head>
  <style>

  div { float:left; }
  span { color:blue; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div><ul>
    <li>John</li>
    <li>Karl</li>
    <li>Brandon</li>

  </ul></div>
  <div><ul>
    <li>Sam</li>
  </ul></div>

  <div><ul>
    <li>Glen</li>
    <li>Tane</li>
    <li>Ralph</li>

    <li>David</li>
  </ul></div>
<script>$("ul li:nth-child(2)").append("<span> - 2nd!</span>");</script>

</body>
</html>

Demo:

Example: This is a playground to see how the selector works with different strings. Notice that this is different from the :even and :odd which have no regard for parent and just filter the list of elements to every other one. The :nth-child, however, counts the index of the child to its particular parent. In any case, it's easier to see than explain so...

<!DOCTYPE html>
<html>
<head>
  <style>
  button { display:block; font-size:12px; width:100px; }
  div { float:left; margin:10px; font-size:10px; 
        border:1px solid black; }
  span { color:blue; font-size:18px; }
  #inner { color:red; }
  td { width:50px; text-align:center; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>
    <button>:nth-child(even)</button>
    <button>:nth-child(odd)</button>
    <button>:nth-child(3n)</button>

    <button>:nth-child(2)</button>
  </div>
  <div>
    <button>:nth-child(3n+1)</button>
    <button>:nth-child(3n+2)</button>

    <button>:even</button>
    <button>:odd</button>
  </div>
  <div><table>

    <tr><td>John</td></tr>
    <tr><td>Karl</td></tr>
    <tr><td>Brandon</td></tr>

    <tr><td>Benjamin</td></tr>
  </table></div>
  <div><table>
    <tr><td>Sam</td></tr>

  </table></div>
  <div><table>
    <tr><td>Glen</td></tr>
    <tr><td>Tane</td></tr>

    <tr><td>Ralph</td></tr>
    <tr><td>David</td></tr>
    <tr><td>Mike</td></tr>

    <tr><td>Dan</td></tr>
  </table></div>
  <span>
    tr<span id="inner"></span>

  </span>
<script>
    $("button").click(function () {
      var str = $(this).text();
      $("tr").css("background", "white");
      $("tr" + str).css("background", "#ff0000");
      $("#inner").text(str);
    });

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :nth-child() Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :nth-child() Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • SJM

    In the description when you say:

    Therefore, given a single containing two s, $(‘li:nth-child(1)’) selects the first while $(‘li:eq(1)’) selects the second.

    should the example for nth-child not be:

    $(‘ul:nth-child(1)’)

    i.e. ‘ul’ not ‘li’??

    • http://www.learningjquery.com/ Karl Swedberg

      No, the example is correct as written. “:nth-child” matches an element that is the nth child of its parent. So, the first li is the first child of the ul.

  • http://benalman.com/ "Cowboy" Ben Alman

    Also note that jQuery :nth-last-child selector is available as a separate plugin, should you need it.

  • Etienne Dupuis

    Ever had a picture gallery or products with thumbnails? We float all the elements but if one picture is higher that the other ones its going to screw up the layout for the next line. We have to clear:left the last element of the line. Instead of doing it server side, this did the trick for me in a snap :

    //5 items per rows – 6th, 12th, 18th etc.. will clear:left;
    $(“.gallery img:nth-child(6n)”).css(“clear”, “left”);

    • http://id.sigurdhsson.org/ Simon Sigurdhsson

      Doing this in actual CSS wasn't good enough?

      • Etienne Dupuis

        We have to deal with Internet Explorer issues.

  • Tom Primožič

    > With :eq(n) only the selector attached to the pseudo-class
    > is counted, not limited to children of any other element,
    > and the nth one is selected.

    The text should say, that “the (n+1)th one is selected”.

    • http://www.learningjquery.com/ Karl Swedberg

      No, I don’t think so. the :eq(n) filter selects the nth (zero-indexed) element.

  • http://twitter.com/davidmirv David Sielert

    This doesn't appear to be 0 indexed to me in the latest version. in a simple table tr:nth-child(0) gives me nothing whilst 1 gives me the first element. Has the functionality been changed?

    • Daniel

      David, as explained in the very beginning, nth-child is 1-indexed.

      So the behavior is correct.

  • Benjiiiiii

    accidently i discover there is a :nth selector
    Since i can't find it in the doc, could some one explain what it does?

    • http://www.learningjquery.com/ Karl Swedberg

      It does the exact same thing as the :eq selector: http://api.jquery.com/eq-selector/. We don't document :nth, because we want people to use :eq so we can eventually phase out :nth.

      • http://pulse.yahoo.com/_ZGJR3THUMLIGBWBYORKCXZHLGU Andy W

        Why do you want to phase out :nth?

        • http://www.learningjquery.com/ Karl Swedberg

          Because it does the exact same thing as :eq, and :eq has always been documented.

  • void

    What would be the way to *correctly* achieve the same result as `:nth-child(-n+3)`? The current behavior gives weird results depending on the scenario.

  • http://twitter.com/sombriks Leonardo Silveira

    so, this thing works with $(“#something”).find(“a:nth-child(2)”) ? whould i try to use any other thing?

  • http://twitter.com/sombriks Leonardo Silveira

    so, this thing works with $(“#something”).find(“a:nth-child(2)”) ? whould i try to use any other thing?

  • Atomikvibes

    If you have this structure :
    <div class=”test”>
    <select>…</select><select>…</select><select>…</select><select>…</select>

    <select>…</select><select>…</select><select>…</select><select>…</select>
    </div>
    …..$(“.test select”).attr(“disabled”,”disabled”);

    I've an error when i try to REactivate only the fisrt and fifth select box.

    $(“.dhx_section_time select:nth-child(4n+1)”).attr(“disabled”,false);

    Did you found this type of error cause by the “span” ???

  • Atomikvibes

    If you have this structure :
    <div class=”test”>
    <select>…</select><select>…</select><select>…</select><select>…</select>
    span…span
    <select>…</select><select>…</select><select>…</select><select>…</select>
    </div>
    …..$(“.test select”).attr(“disabled”,”disabled”);

    I've an error when i try to REactivate only the fisrt and fifth select box.

    $(“.test select:nth-child(4n+1)”).attr(“disabled”,false);

    Did you found this type of error cause by the “span” ???

  • banningstuckey

    $('table#tableID > tbody > tr').each(function(index){
    console.log(index);
    $('table#tableID > tbody > tr:nth-child('+index+') > td').each(function(index){

    kept breaking in Chrome and Safari… ended up it was the nth-child part breaking so i replaced it with :eq and it work.

    Just thought i'd share in case anyone else out there has the same problem.