How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Select the Last Child Element in jQuery
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Select the Last Child Element in jQuery
Web Development

How to Select the Last Child Element in jQuery

how7o
By how7o
Last updated: May 23, 2026
4 Min Read
Select the last child element with jQuery
SHARE

To select the last child element with jQuery, use $('.parent').children().last() for the last child of any type, or $('.parent p').last() for the last <p> specifically. The selectors :last and :last-child do similar but subtly different things — pick by the case below.

Contents
  • The three equivalent forms
  • When the choice actually matters
  • Use it to do something
  • Vanilla JS equivalent
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with jQuery 3.7. Originally published 2022-09-17, rewritten and updated 2026-05-17.

The three equivalent forms

<div class="article">
  <p>Some random text 1</p>
  <p>Some random text 2</p>
  <!-- ... -->
  <p>Some random text 6</p>
</div>
// Children — method form (recommended)
$('.article').children().last();

// :last — jQuery extension
$('.article p:last');

// :last-child — CSS pseudo-class
$('.article p:last-child');

All three return the last <p> in the example. The differences matter when the DOM gets messier — see below.

jQuery select last child — .last(), :last, :last-child, :last-of-type compared

When the choice actually matters

<div class="article">
  <p>Some random text 1</p>
  <p>Some random text 2</p>
  <p>Some random text 3</p>
  <footer>Last footer</footer>   <!-- last child of .article -->
</div>
  • $('.article p').last() → “Some random text 3” (last <p>, regardless of position)
  • $('.article p:last') → “Some random text 3” (same — :last on the matched set)
  • $('.article p:last-child') → nothing (no <p> is the literal last child of .article; the <footer> is)
  • $('.article p:last-of-type') → “Some random text 3” (last <p> among its siblings of the same type)

If you want “the last <p> regardless of what comes after,” reach for .last() or :last-of-type — not :last-child.

Use it to do something

// Add a class
$('.article p').last().addClass('final');

// Read its text
const lastPara = $('.article p').last().text();

// Append something after it
$('.article p').last().after('<p class="footnote">See references.</p>');

Vanilla JS equivalent

// Last 

inside .article document.querySelector('.article p:last-of-type'); // Or by collecting all and taking the last index const paras = document.querySelectorAll('.article p'); const last = paras[paras.length - 1]; // Last child element of any type document.querySelector('.article').lastElementChild;

Frequently asked questions

What’s the difference between :last and :last-child?

:last is a jQuery extension that picks the last element from the matched set. :last-child is a standard CSS pseudo-class — it matches an element only if it’s the last child of its parent. For .article p:last vs .article p:last-child: if the last child of .article isn’t a <p>, :last-child matches nothing, but :last still returns the last <p>. Subtle and surprising — usually you want :last.

Is :last slower than :last-child?

Yes — :last-child is a native CSS selector that the browser optimises (it uses document.querySelectorAll internally). :last requires jQuery to fetch every match and pick the last, so it can’t be passed to the browser’s native selector engine. For most pages the difference is negligible; on large DOMs with thousands of matches it can matter.

How do I select the last matching element, not just the literal last child?

Use .last(): $('.article p').last() grabs the last <p> inside .article, regardless of whether it’s the literal last child. Pairs cleanly with .first() for symmetry. :last selector and .last() method are equivalent.

What’s the native JavaScript way?

document.querySelector('.article p:last-of-type') for “the last <p> inside .article” — same idea as jQuery’s :last-of-type. Or document.querySelectorAll('.article p') followed by nodes[nodes.length - 1]. Both are widely supported, no library needed.

Related guides

  • How to Get the Index in a jQuery .each() Loop
  • How to Check if an Element Is Visible or Hidden with jQuery
  • How to Scroll to an Element Using jQuery

References

jQuery .last(): api.jquery.com/last. jQuery :last: api.jquery.com/last-selector. MDN :last-child: developer.mozilla.org/en-US/docs/Web/CSS/:last-child. MDN :last-of-type: developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type.

TAGGED:domJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Select all text in a contenteditable div on click How to Select All Text in a Contenteditable Div on Click
Next Article Use a JavaScript variable as an object key How to Use a Variable as an Object Key in JavaScript
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Laravel leftJoin to keep all records even without matches
Web Development

How to Use LEFT JOIN in Laravel to Keep All Records

4 Min Read
WordPress default posts per page — get_option reads the Settings Reading value
Web Development

How to Get the Default Posts Per Page Value in WordPress

5 Min Read
Laravel DataTables HTML column — rawColumns opt-out of the default escaping
Web Development

How to Add an HTML Column in Laravel DataTables

7 Min Read
Run JS code on URL hash change with the hashchange event
Web Development

How to Run Code on URL Hash Change in JavaScript

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?