• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery Get Last Child

jQuery Get Last Child

January 30, 2022 Leave a Comment

To get the last child of a HTML element using jQuery, the simplest way is with the jQuery :last-child selector.

$("#div p:last-child");

Let’s say I have the following HTML:

<div id="div1">
   <p>This is the first child of #div1</p>
   <p>This is the second child of #div1</p>
   <p>This is the third child of #div1</p>
   <p>This is the fourth child of #div1</p>
</div>

To get the last paragraph child of the div, we can use the jQuery :last-child selector:

$("#div1 p:last-child");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1 p:last-child");

How to Get the Last Child on Click using jQuery

We can get the last child of an HTML element using jQuery very easily by combining the :last-child selector with a click event.

Let’s say we have the following HTML code and we want to change the background color of the last paragraph.

<div id="div1">
  <p>This is paragraph 1</p>
  <p>This is paragraph 2</p>
  <p>This is paragraph 3</p>
</div>
<div id="click-me">Change background</div>

We can utilize the jQuery :last-child selector, and the jQuery css() method to change the background of the last paragraph.

Below is the Javascript code which will allow the user to be able to select the last paragraph and set the new background color using jQuery.

$("#click-me").click(function(){
    $("#div1 p:last-child").css("background","#c1e9c1");
}); 

The final code and output for this example of how to change the background of the last child of a div using jQuery and Javascript is below:

Code Output:

This is paragraph 1

This is paragraph 2

This is paragraph 3

Change background

Full Code:

<div id="div1">
    <p>This is paragraph 1</p>
    <p>This is paragraph 2</p>
    <p>This is paragraph 3</p>
</div>
<div id="click-me">Change background</div>

<script>

$("#click-me").click(function(){
    $("#div1 p:last-child").css("background","#c1e9c1");
});

</script>

Hopefully this article has been useful to help you understand how to use jQuery to get the last child from a parent HTML element.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Select Multiple Ids
  • 2.  Get nth Child with jQuery
  • 3.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 4.  Using jQuery to Check if Checkbox is Checked
  • 5.  jQuery next – Get the Next Element of a Div
  • 6.  jQuery Display None
  • 7.  jQuery mouseenter – An Interactive Example of the mouseenter Method
  • 8.  Using jQuery to Get the Current URL
  • 9.  Using jQuery to Swap an Image on Hover
  • 10.  Using jQuery to Move Element Before Another

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy