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
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Securely hash passwords in PHP with password_hash
Web Development

Securely Hash Passwords in PHP (password_hash, Argon2id)

6 Min Read
Laravel Eloquent orderBy — code snippet sorting posts by id descending with arrow icons
Web Development

How to Use orderBy in Laravel Eloquent (with Examples)

6 Min Read
Fix CORS policy blocked origin errors in PHP and Apache
Web Development

How to Fix “CORS Policy Blocked Origin” Errors

5 Min Read
Laravel database transactions explained
Web Development

Laravel Database Transactions Explained (DB::transaction vs beginTransaction)

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