jQuery :last-of-type Selector

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

What Is :last-of-type?

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

Remember
// 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");
NeedUse
Last of a tag type$("span:last-of-type")
Last child (any tag)$("span:last-child")
Last in a jQuery set$("span").last()
Nth from end by type$("p:nth-last-of-type(2)")
First of a tag type$("span:first-of-type")

How to Use :last-of-type

Scope the selector, then style or read the matched last-of-type elements.

  1. Write a typed selector such as $("span:last-of-type") or $("p:last-of-type").
  2. Optionally scope with a parent: $("article p:last-of-type").
  3. Chain methods — .text(), .addClass(), .css(), events.
  4. Remember: only siblings with the same tag are compared.
  5. Use :last-child when the element must also be the final child.
PieceRequired?Role
Tag / filterRecommendede.g. span, p, td
:last-of-typeYesLast sibling of that tag name
Parent scopeOptionalLimits which trees are searched

Pseudocode

Pseudocode
for each element matching the left side (e.g. span):
    look at its parent
    find all siblings with the same tag name
    if this element is the last of those siblings:
        include it in the result

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

.compare with :last-child:
    the element must also be the final child
    of any tag type under that parent

Cheat Sheet

Keep these facts handy while selecting last-of-type elements.

TopicDetail
Selector$("elem:last-of-type")
SincejQuery 1.9
Matches0–many (one per parent + tag)
Equivalent:nth-last-of-type(1)
vs :last-childSame tag only vs final child of any tag
vs .last()Per parent + type vs one from the set
CSS?Yes — standard CSS pseudo-class
Official demo$("span:last-of-type")

Live Preview

Mark last-of-type spans, compare with :last-child, or count matches.

Sample blocks below. A trailing p blocks :last-child on spans, not :last-of-type.

Corey Yehuda Todd

note

Jörn Scott Timo
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 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.
  4. Count. Two divs → two last-of-type spans.

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.
  4. Pick. Use of-type when other tags may trail; use last-child when position must be final.

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.
  4. Reuse. Same idea works for cards, posts, and FAQ answers.

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

Both selectors return the same matched set.

jQuery
$("#out").text(
  ":last-of-type → " + $("ul li:last-of-type").length +
  " | :nth-last-of-type(1) → " + $("ul li:nth-last-of-type(1)").length
);
Try It Yourself

How It Works

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

Example 5 — Last Cell in Each Row

Select the last td of its type in every table row.

jQuery
$("tr td:last-of-type").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 td. td:last-of-type is the last td among siblings.
  3. Mark. An asterisk is appended for a clear visual cue.
  4. Note. In a row of only td cells, this matches td:last-child too.

Edge Cases & Pitfalls

Check these when a last-of-type selector surprises you.

siblings

Compares same tag only

A following p does not stop span:last-of-type. Use :last-child if the element must be the final child.

vs

Not the same as .last()

.last() returns one element from the set. :last-of-type is about tag position among siblings.

scope

Bare :last-of-type is broad

Prefer $("p:last-of-type") or a parent scope so you do not restyle every last-of-type node on the page.

version

Needs jQuery 1.9+

Older jQuery builds before 1.9 do not support :last-of-type. Upgrade or use a different filter.

Browser Support

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

✓ Stable · jQuery 1.9+

jQuery :last-of-type

Works in every browser that runs jQuery 1.9+. Behavior matches the CSS :last-of-type 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-of-type Universal

Bottom line: Safe for everyday UI. Prefer typed selectors (span:last-of-type) and remember it can match once per parent for that tag.

Performance Notes

ApproachCostBest for
Scoped $("article p:last-of-type")CheapArticles, cards, local blocks
Typed $("span:last-of-type")Cheap–moderatePage-wide last-of-type tags
Bare $(":last-of-type")ModerateAvoid — too broad

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

Key Takeaways

  • Role: match the last sibling of each tag name under a parent.
  • Many matches: one last-of-type is possible for each parent + tag.
  • vs :last-child: same tag only vs must be the final child.
  • Scope: prefer $("p:last-of-type") over bare $(":last-of-type").

One line: :last-of-type picks the last sibling of a given tag — even if another tag type follows.

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