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

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:nth-last-of-type(index) selects elements that are the nth sibling of their tag name, counting from the last same-type sibling. Other tags in between do not change the count. Available since jQuery 1.9.

Syntax

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

jQuery
$(":nth-last-of-type(index | even | odd | equation)")

$("li:nth-last-of-type(1)")     → last li (≡ :last-of-type)
$("li:nth-last-of-type(2)")     → second-to-last li
$("span:nth-last-of-type(1)")   → last span among siblings

$("li:nth-last-child(2)")       → 2nd-to-last among ALL children
$("li:nth-of-type(2)")          → 2nd li from the start
CallWhat it does
:nth-last-of-type()Learn the jQuery :nth-last-of-type() selector: 1-based reverse index among same-tag siblings, vs :nth-last-child(), even/odd, An+B, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Choose the element type (li, p, span, and so on).
Step 2Pass a 1-based index from the last same-type sibling, or even/odd/An+B.
Step 3Ignore other tag names when counting — they do not shift of-type indexes.

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-of-type(2)

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

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

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

How It Works

  1. Group by tag. Only li siblings are numbered.
  2. Count from the end. Last li is 1; previous li is 2.
  3. One per list. Each ul contributes its penultimate li.

Example 2 — Last of Type: (1) ≡ :last-of-type

Show that index 1 from the end is the last element of that type.

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

var b = $("ul li:last-of-type").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 is always the last li in the parent.
  2. Compare with :last-of-type. Same nodes on the same markup.
  3. Prefer the short form. Use :last-of-type when you only need the last of that tag.

Example 3 — Of-Type vs :nth-last-child()

Mixed tags show why same-type counting matters.

jQuery
// Markup: <span>A</span> <p>note</p> <span>B</span>
console.log("of-type:", $("span:nth-last-of-type(1)").text());
console.log("last-child:", $("span:nth-last-child(1)").length);

// of-type → "B" (last span)
// last-child → 0 (last child is the p)
Try It Yourself

How It Works

  1. Of-type group. Spans are numbered A=2, B=1 from the end.
  2. Last span wins. span:nth-last-of-type(1) selects B.
  3. All-children check. The last child is p, so span:nth-last-child(1) fails.

Frequently Asked Questions

:nth-last-of-type(index) selects elements that are the nth sibling of their tag name when counting backward from the last same-type sibling. $("li:nth-last-of-type(2)") matches the second-to-last li under each parent — counting only li elements.
:nth-last-child() counts every sibling element. :nth-last-of-type() counts only siblings with the same tag name. Mixed markup (span next to p) is where they diverge.
Yes. Both select the last element of that tag name among siblings under the same parent.
Yes. Per CSS and jQuery docs, counting starts at 1 from the last same-type sibling. li:nth-last-of-type(1) is the last li.
Official docs support :nth-last-of-type(even), odd, 3n, and 3n+1 — evaluated among same-tag siblings from the end, ignoring other element types.
jQuery 1.9. It follows the W3C CSS specification and works via querySelectorAll in modern browsers.

Next: :only-of-type Selector

Select elements that are the only sibling of their element type.

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