jQuery :last-child Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

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

jQuery
// 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();
CallWhat it does
:last-childLearn jQuery :last-child: select elements that are the last child of their parent, compare with .last() and :last-of-type, and style last items in lists and tables.

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

Parameters

ParameterDescription
Step 1Write a typed selector such as $("li:last-child") or $("div span:last-child").
Step 2Optionally scope with a parent: $("ul.nav li:last-child").
Step 3Chain methods — .text(), .addClass(), .css(), events.

Return Value

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

jQuery Examples

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.

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.

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.

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

8 people found this page helpful