jQuery :last-of-type Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:last-of-type (since jQuery 1.9) selects every element that is the last among siblings of the same tag name. Unlike :last-child, a different tag after it does not block the match.

Syntax

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

jQuery
// Official pattern — last span among siblings
$("span:last-of-type");

// Last paragraph in each article
$("article p:last-of-type");

// Same idea as :nth-last-of-type(1)
$("li:nth-last-of-type(1)");

// Stricter: must also be the final child
$("span:last-child");
CallWhat it does
:last-of-typeLearn jQuery :last-of-type: select the last sibling of each tag name, compare with :last-child and .last(), and style closing spans, paragraphs, and table cells.

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 $("span:last-of-type") or $("p:last-of-type").
Step 2Optionally scope with a parent: $("article p:last-of-type").
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 span:last-of-type

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

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

How It Works

  1. Select. span:last-of-type finds the last span among siblings.
  2. Per parent. Each div contributes at most one match for that tag.
  3. Update. .text() prefixes the matched names.

Example 2 — :last-of-type vs :last-child

A trailing paragraph blocks span:last-child, not span:last-of-type.

jQuery
$("span:last-of-type").text(function (i, t) {
  return t + " (of-type)";
});
$("span:last-child").text(function (i, t) {
  return t + " (last-child)";
});

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

How It Works

  1. Markup. Two spans, then a p footer under one parent.
  2. :last-of-type. Beta is still the last span sibling.
  3. :last-child. No span is last child — the p is.

Example 3 — Closing Paragraph in Each Article

Scope with article so only in-article paragraphs match.

jQuery
$("article p:last-of-type").text(function (i, t) {
  return t + " ← closing";
});
Try It Yourself

How It Works

  1. Scope. article limits the search to each article.
  2. Filter. p:last-of-type picks the last paragraph sibling.
  3. Label. Text is appended so the match is easy to see.

Frequently Asked Questions

:last-of-type selects every element that is the last among siblings with the same element name (tag). $("span:last-of-type") matches the last span in each parent. Available since jQuery 1.9.
:last-child requires the element to be the last child among all element types. :last-of-type only compares siblings with the same tag. If a <p> comes after a <span>, the span is not :last-child but can still be span:last-of-type.
Yes. In CSS, :last-of-type is equivalent to :nth-last-of-type(1). Both select the last element of that tag name among siblings under the same parent.
.last() returns only one element — the last in the entire jQuery matched set. :last-of-type can return many — one per parent for that tag type.
Another element type may be the last child. Example: <div><span>A</span><span>B</span><p>Footer</p></div> — the last span matches span:last-of-type but not span:last-child because the paragraph is the last child.
Yes — $("p:last-of-type") or $("article p:last-of-type") is clearer than bare $(":last-of-type"). Scoping to a parent class or ID avoids matching unintended nodes across the page.

Next: :nth-last-of-type() Selector

Count same-tag siblings from the end of each parent.

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

8 people found this page helpful