:nth-last-child(index) selects elements that are the nth child of their parent, counting from the last sibling. Counting is 1-based (CSS) and includes every sibling element. Available since jQuery 1.9.
Remember
$(":nth-last-child(index | even | odd | equation)")
$("li:nth-last-child(1)") → last child (≡ :last-child)
$("li:nth-last-child(2)") → second-to-last child
$("li:nth-last-child(even)") → even positions from the end
$("li:nth-last-child(3n)") → 3rd, 6th… from the end
$("li:nth-child(2)") → 2nd from the start
$("li").eq(-1) → last in a flat jQuery set
Attach the pseudo-class to a tag or class, then pass an index, keyword, or equation — counted from the end.
Choose the element type you care about (li, tr, and so on).
Pass a 1-based index from the end, even/odd, or an An+B equation.
Remember: all siblings count — not only matching tags.
Use :nth-child() if you need positions from the start instead.
Piece
Required?
Role
Base selector
Usually
Element that must match at that child slot
:nth-last-child(…)
Yes
Child-position filter from the end
Argument
Yes
Index, even/odd, or equation
Pseudocode
Pseudocode
for each element E matching base:
parent = E.parent
indexFromEnd = 1-based position counting backward
among parent's children
if indexFromEnd matches (n | even | odd | An+B):
keep E
return jQuery(kept)
Reference
Cheat Sheet
Keep these facts handy while counting from the end.
Topic
Detail
Syntax
$(":nth-last-child(n | even | odd | An+B)")
Counting
1-based from the last sibling
(1)
Same as :last-child
(2)
Second-to-last child
vs :nth-child()
End → start vs start → end
Since
jQuery 1.9 (CSS)
Try it
Live Preview
Pick an :nth-last-child() pattern against two short lists and see which items match.
Two lists — matches can appear once per parent, like the official demo.
John
Karl
Adam
Dan
Dave
Rick
Timmy
Gibson
Result
$("ul li:nth-last-child(2)")
matched: 2
text: Adam, Timmy
/* 1-based position counting from the end */
Code
jQuery Examples
Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.
Example 1 — Official Demo: ul li:nth-last-child(2)
Mark the second-to-last list item in every ul (jQuery API example).
One line:$("li:nth-last-child(n)") means “the nth child from the end — if that child is an li.”
Frequently Asked Questions
:nth-last-child(index) selects elements that are the nth child of their parent when counting from the last sibling backward. $("li:nth-last-child(2)") matches the second-to-last li in each list — if that li occupies that child slot.
Yes. Like :nth-child(), counting is 1-indexed per CSS. :nth-last-child(1) is the last child; :nth-last-child(2) is the penultimate child.
Yes. Both select an element that is the last child of its parent. Use :nth-last-child() when you need positions further from the end.
:nth-child(n) counts from the first sibling forward; :nth-last-child(n) counts from the last sibling backward. Same formula syntax, opposite direction.
Official docs support :nth-last-child(even), odd, 3n, and 3n+1 — the same An+B formulas as :nth-child(), evaluated from the end of each parent's child list.
jQuery 1.9. It follows the W3C CSS specification and works via querySelectorAll in modern browsers.