How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Select the Last Child Element in jQuery
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • 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.

You Might Also Like

Laravel get config variable — config() helper and Config facade resolving dotted keys
Web Development

How to Get Config Variables in Laravel

7 Min Read
WooCommerce get customer ID from order — WC_Order::get_user_id
Web Development

How to Get the Customer ID from an Order ID in WooCommerce

7 Min Read
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
Web Development

How to Add Foreign Keys in Laravel Migration

6 Min Read
Create a folder in PHP if it does not already exist
Web Development

How to Create a Folder If It Does Not Exist in PHP

5 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • 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?