jQuery :nth-last-child() Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

: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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$(":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
CallWhat it does
Base selectorElement that must match at that child slot
:nth-last-child(…)Child-position filter from the end
ArgumentIndex, even/odd, or equation

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Choose the element type you care about (li, tr, and so on).
Step 2Pass a 1-based index from the end, even/odd, or an An+B equation.
Step 3Remember: all siblings count — not only matching tags.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

Example 1 — Official Demo: ul li:nth-last-child(2)

Mark the second-to-last list item in every ul (jQuery API example).

jQuery
$("ul li:nth-last-child(2)").each(function () {
  this.textContent = this.textContent + " - 2nd to last!";
});

console.log($("ul li:nth-last-child(2)").length);
Try It Yourself

How It Works

  1. Scan each list. Every ul has its own child positions from the end.
  2. Require index 2 from the end. The penultimate child is the candidate.
  3. Require an li. That child must also be a list item.

Example 2 — Last Child: :nth-last-child(1) ≡ :last-child

Show that index 1 from the end is the last child.

jQuery
var a = $("ul li:nth-last-child(1)").map(function () {
  return this.textContent.trim();
}).get().join(", ");

var b = $("ul li:last-child").map(function () {
  return this.textContent.trim();
}).get().join(", ");

console.log(a);
console.log(b);
console.log(a === b);
Try It Yourself

How It Works

  1. Index 1 from the end. That slot is always the last child.
  2. Compare with :last-child. Same nodes on the same markup.
  3. Prefer the short form. Use :last-child when you only need the last item.

Example 3 — Even / Odd from the End

Stripe by child position counted backward inside each parent.

jQuery
console.log("even:", $("ul li:nth-last-child(even)").length);
console.log("odd:", $("ul li:nth-last-child(odd)").length);

$("ul li:nth-last-child(even)").each(function () {
  this.textContent = "[E] " + this.textContent;
});
Try It Yourself

How It Works

  1. Count backward. Last child is position 1 (odd); second-to-last is 2 (even).
  2. Even from the end. Positions 2, 4, 6… match even.
  3. Odd from the end. Positions 1, 3, 5… match odd.

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.

Next: :first-of-type Selector

Select the first element of its type among siblings with :first-of-type.

:first-of-type Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful