: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.
Remember
$(":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
Pick a tag, then pass an index, keyword, or equation counted among that tag only — from the end.
Choose the element type (li, p, span, and so on).
Pass a 1-based index from the last same-type sibling, or even/odd/An+B.
Ignore other tag names when counting — they do not shift of-type indexes.
Use :nth-last-child() if every sibling type should count.
Piece
Required?
Role
Base / type
Usually
Tag name that defines the of-type group
:nth-last-of-type(…)
Yes
Reverse index within that type
Argument
Yes
Index, even/odd, or equation
Pseudocode
Pseudocode
for each element E matching base:
sameType = siblings of E.parent with tagName === E.tagName
indexFromEnd = 1-based position of E in sameType
counting from the last same-type sibling
if indexFromEnd matches (n | even | odd | An+B):
keep E
return jQuery(kept)
Reference
Cheat Sheet
Keep these facts handy while counting same-type siblings from the end.
Topic
Detail
Syntax
$(":nth-last-of-type(n | even | odd | An+B)")
Counting
1-based among same tag, from the end
(1)
Same as :last-of-type
vs :nth-last-child()
Same type only vs all children
vs :nth-of-type()
From end vs from start
Since
jQuery 1.9 (CSS)
Try it
Live Preview
Pick a pattern against two lists (and a mixed block) to see of-type matches from the end.
Lists mirror the official demo; the mixed block shows of-type vs last-child.
John
Karl
Adam
Dan
Dave
Rick
Timmy
Gibson
A
note
B
Result
$("ul li:nth-last-of-type(2)")
matched: 2
text: Adam, Timmy
/* same-tag siblings, counted from the end */
Code
jQuery Examples
Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.
Example 1 — Official Demo: ul li:nth-last-of-type(2)
Mark the second-to-last li in every list (jQuery API example).
One line:$("li:nth-last-of-type(n)") means “the nth li from the end among li siblings.”
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.