• 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 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 on change
  • 2.  Changing the Background Image of a div Using jQuery
  • 3.  How to Use jQuery to Animate the Font Size of a Paragraph
  • 4.  Using jQuery to Replace HTML Inside Div
  • 5.  Using jQuery to Select Multiple Classes
  • 6.  Using jQuery to Get the Bottom Position of Element
  • 7.  Using jQuery to Replace a Class
  • 8.  jQuery get img src – Get the Source of an Image Using jQuery
  • 9.  jQuery Random Number Generator Between Two Numbers
  • 10.  jQuery has – Check if an Element Has Other Elements

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