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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / jQuery / jQuery last() – Get the Last Element

jQuery last() – Get the Last Element

February 3, 2022 Leave a Comment

With the jQuery last() method, we can get the last element or div in a series of elements or divs.

$("div").last();

Let’s say we have the following HTML:

<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>

As we can see, we have three divs with class “divs”, each of which has two paragraphs. If we want to get the last div with class “divs” and change only its background color, we can do this using the jQuery last() method.

Here is the JavaScript code that would accomplish this:

$(".divs").last().css("background","green");

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

jQuery(".divs").last().css("background","green");

Using jQuery to Get the Last Div and Change its Background

In this example, we will get the last div of a series of divs with the same class using the jQuery last() method, and change its background color.

Here is our HTML setup with some styles added to the divs:

<style>.divs { border: 1px solid #444; margin-bottom: 20px; padding: 10px; }</style>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div id="click-me">Change background</div>

We can utilize the jQuery last() method, and the jQuery css() method to change the background of the last div.

Below is the Javascript code which will allow the user to be able to change the last div’s background color using jQuery.

$("#click-me").click(function(){
  $(".divs").last().css("background","#c1e9c1");
});

The final code and output for this example is below:

Code Output:

This is some text

This is some text

This is some text

This is some text

This is some text

This is some text

Change background

Full Code:

<style>.divs { border: 1px solid #444; margin-bottom: 20px; padding: 10px; }</style>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div id="click-me">Change background</div>

<script>

$("#click-me").click(function(){
  $(".divs").last().css("background","#c1e9c1");
});

</script>

Hopefully this article has been useful to help you understand the jQuery last() method.

Other Articles You'll Also Like:

  • 1.  jQuery mouseup – An Interactive Example of the jQuery mouseup Method
  • 2.  jQuery insertAfter – Insert Element After Another Element
  • 3.  Using jQuery to Show a Div
  • 4.  Using jQuery to Delete an Element
  • 5.  Using jQuery to Set the Width of a Div
  • 6.  Using jQuery to Move Element Before Another
  • 7.  Toggle the Visibility of an Element Using jQuery
  • 8.  jQuery mouseenter – An Interactive Example of the mouseenter Method
  • 9.  Using jQuery to Get the Current URL
  • 10.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery

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