jQuery :last-child Selector

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is :last-child?

:last-child (since jQuery 1.1.4) selects every element that is the last child of its parent. Unlike .last(), which returns one element from a set, :last-child can match one per parent.

Remember
// Official pattern — last span in each div
$("div span:last-child");

// Last list item in each ul
$("ul li:last-child");

// Same idea as :nth-last-child(1)
$("p:nth-last-child(1)");

// Not the same as .last() (one element only)
$("li").last();
NeedUse
Last child per parent$("li:last-child")
Last in a jQuery set$("li").last()
Last of a tag type$("span:last-of-type")
Nth from the end$("li:nth-last-child(2)")
First child$("li:first-child")

How to Use :last-child

Scope the selector, then style or read the matched last children.

  1. Write a typed selector such as $("li:last-child") or $("div span:last-child").
  2. Optionally scope with a parent: $("ul.nav li:last-child").
  3. Chain methods — .text(), .addClass(), .css(), events.
  4. Remember: the element must be the final element child of its parent.
  5. Use .last() only when you want one match from the whole set.
PieceRequired?Role
Tag / filterRecommendede.g. li, span, td
:last-childYesMust be the last element sibling
Parent scopeOptionalLimits which trees are searched

Pseudocode

Pseudocode
for each element matching the left side (e.g. span):
    look at its parent
    if this element is the last element child of that parent:
        include it in the result

result may contain many elements
    → one match is possible for each parent

.compare with .last():
    take the full matched set
    keep only the final item in that set
    → always 0 or 1 element

Cheat Sheet

Keep these facts handy while selecting last children.

TopicDetail
Selector$("elem:last-child")
SincejQuery 1.1.4
Matches0–many (one per parent possible)
Equivalent:nth-last-child(1)
vs .last()Per parent vs one from the set
vs :last-of-typeLast of any tag vs last of same tag
CSS?Yes — standard CSS pseudo-class
Official demo$("div span:last-child")

Live Preview

Mark the last child in each list, compare with .last(), or count matches.

Sample lists below. :last-child can match once per parent.

  • A1
  • A2
  • A3
  • B1
  • B2
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Official div span:last-child

Mark the last span in each div (official demo idea, CSS-free).

jQuery
$("div span:last-child").text(function (i, text) {
  return "[last] " + text;
});
Try It Yourself

How It Works

  1. Select. div span:last-child finds last spans inside each div.
  2. Per parent. Each div contributes at most one match.
  3. Update. .text() prefixes the matched names.
  4. Count. Two divs → two last-child spans.

Example 2 — :last-child vs .last()

Official distinction: many per-parent matches vs one global match.

jQuery
$("li:last-child").text(function (i, t) {
  return t + " (last-child)";
});
$("li").last().text(function (i, t) {
  return t + " [.last()]";
});

$("#out").text(
  ":last-child → " + $("li:last-child").length +
  " | .last() → " + $("li").last().length
);
Try It Yourself

How It Works

  1. :last-child. Matches the last li in each ul.
  2. .last(). Takes all li elements, keeps only the final one.
  3. Count. Two lists → two last-children; .last() stays at 1.
  4. Pick. Use :last-child for per-group UI; .last() for one item.

Example 3 — Last Item in Each Nav List

Scope with a class so only ul.nav lists are affected.

jQuery
$("ul.nav li:last-child").text(function (i, t) {
  return t + " ← last";
});
Try It Yourself

How It Works

  1. Scope. ul.nav limits the search to nav lists.
  2. Filter. li:last-child picks the final item in each.
  3. Label. Text is appended so the match is easy to see.
  4. Reuse. Same idea works for menus, steps, and card footers.

Example 4 — Same as :nth-last-child(1)

Both selectors return the same matched set.

jQuery
$("#out").text(
  ":last-child → " + $("div p:last-child").length +
  " | :nth-last-child(1) → " + $("div p:nth-last-child(1)").length
);
Try It Yourself

How It Works

  1. Equivalence. In CSS, :last-child equals :nth-last-child(1).
  2. Count. Both lengths should match for the same markup.
  3. Choose. Prefer :last-child for readability.
  4. Extend. Use :nth-last-child(n) when you need 2nd-from-last, etc.

Example 5 — Last Cell in Each Row

Select the last td in every table row.

jQuery
$("tr td:last-child").text(function (i, t) {
  return t + " *";
});
Try It Yourself

How It Works

  1. Row root. Each tr is a parent of its cells.
  2. Last cell. td:last-child is the final cell in that row.
  3. Mark. An asterisk is appended for a clear visual cue.
  4. Note. Header cells use th — this selector targets td only.

Edge Cases & Pitfalls

Check these when a last-child selector returns nothing.

siblings

Must be the final element child

If another element follows your target, :last-child will not match — try :last-of-type instead.

vs

Not the same as .last()

.last() returns one element from the set. :last-child is about parent/child position.

scope

Bare :last-child is broad

Prefer $("li:last-child") or a parent scope so you do not restyle every last child on the page.

text

Text nodes do not count

Whitespace text between tags is ignored for child-index — only element nodes matter.

Browser Support

:last-child is a standard CSS pseudo-class. jQuery has supported it since 1.1.4. It also works with native querySelectorAll in modern browsers.

✓ Stable · jQuery 1.1.4+

jQuery :last-child

Works in every browser that runs your loaded jQuery version. Behavior matches the CSS :last-child rule.

100% CSS selector
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:last-child Universal

Bottom line: Safe for everyday UI. Prefer typed selectors (li:last-child) and remember it can match once per parent.

Performance Notes

ApproachCostBest for
Scoped $("ul.nav li:last-child")CheapMenus, cards, local lists
Typed $("li:last-child")Cheap–moderatePage-wide last items
Bare $(":last-child")ModerateAvoid — too broad

Child filters are fast when scoped. Start from a known parent when the page is large.

Key Takeaways

  • Role: match elements that are the last child of their parent.
  • Many matches: one last-child is possible for each parent.
  • vs .last(): position in the DOM vs position in a jQuery set.
  • Scope: prefer $("li:last-child") over bare $(":last-child").

One line: :last-child picks the final element child of each parent — not just the last item in a jQuery set.

Frequently Asked Questions

:last-child selects every element that is the last child of its parent. $("div span:last-child") matches the last span inside each div — so multiple elements can match. Available since jQuery 1.1.4.
.last() returns only one element — the last in the entire jQuery matched set. :last-child can return many — one per parent. The official jQuery docs highlight this distinction.
Yes. In CSS, :last-child is equivalent to :nth-last-child(1). Both select elements that are the last child of their parent.
:last-child requires the element to be the final child among all siblings. :last-of-type selects the last sibling of that tag name, even if other tag types follow it.
If another element sibling comes after your target, it will not match. Only the final element child of the parent matches, regardless of tag name. Text nodes do not count as element children.
Yes — $("li:last-child") or $("div span:last-child") is clearer than bare $(":last-child"). Scoping to a parent class or ID avoids matching unintended last children across the page.

Next: :only-child selector

Select elements that are the only child of their parent.

:only-child tutorial →

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.

8 people found this page helpful