Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery Basic

jQuery Ajax Events

jQuery Ajax Methods

jQuery Keyboard Events

jQuery Keyboard Methods

jQuery Form Events

jQuery Form Methods

jQuery Mouse Event

jQuery Mouse Methods

jQuery Event Object

jQuery Fading

jQuery Document Loading

jQuery Traversing

jQuery Utilities

jQuery Property

jQuery HTML

jQuery CSS

jQuery Miscellaneous

jQuery .last() Method

Posted in jQuery Tutorial
Updated on May 10, 2024
By Mari Selvan
👁️ 15 - Views
⏳ 4 mins
💬 0
jQuery .last() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery offers a plethora of methods to manipulate HTML elements and traverse the DOM tree efficiently. Among these methods is .last(), which allows you to select the last element of a collection matched by a selector or jQuery object. Understanding and utilizing the .last() method can streamline your development process and enhance the interactivity of your web pages.

In this guide, we'll delve into the functionality of the jQuery .last() method, accompanied by clear examples to illustrate its usage.

🧠 Understanding .last() Method

The .last() method in jQuery is used to select the last element of a matched set of elements. It can be applied to a jQuery object or a collection of DOM elements obtained through a selector.

💡 Syntax

The syntax for the .last() method is straightforward:

syntax.js
Copied
Copy To Clipboard
$(selector).last()

📝 Example

  1. Selecting the Last Element:

    Suppose you have a list of items and you want to select and manipulate the last item. You can achieve this using the .last() method as follows:

    index.html
    Copied
    Copy To Clipboard
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
    example.js
    Copied
    Copy To Clipboard
    $("li").last().css("font-weight", "bold");

    This will make the text of the last <li> element bold.

  2. Chaining with Other jQuery Methods:

    The .last() method can be seamlessly chained with other jQuery methods to perform various operations. For instance, let's add a class to the last paragraph in a <div> element:

    index.html
    Copied
    Copy To Clipboard
    <div>
      <p>Paragraph 1</p>
      <p>Paragraph 2</p>
      <p>Paragraph 3</p>
    </div>
    example.js
    Copied
    Copy To Clipboard
    $("div").find("p").last().addClass("last-paragraph");

    This will add the class last-paragraph to the last <p> element within the <div>.

  3. Applying Conditional Logic with .last():

    You can use the .last() method in conjunction with conditional statements to target specific elements based on certain criteria. For example, let's change the background color of the last visible <div> element:

    index.html
    Copied
    Copy To Clipboard
    <div style="display: none;">Hidden Div 1</div>
    <div>Visible Div 1</div>
    <div>Visible Div 2</div>
    <div style="display: none;">Hidden Div 2</div>
    example.js
    Copied
    Copy To Clipboard
    $("div:visible").last().css("background-color", "lightblue");

    This will set the background color of the last visible <div> element to light blue.

🎉 Conclusion

The jQuery .last() method provides a convenient way to target and manipulate the last element of a selected set of elements. Whether you need to style the last item, chain it with other jQuery methods, or apply conditional logic, the .last() method offers flexibility and efficiency in DOM traversal and manipulation.

By incorporating this method into your development workflow, you can enhance the interactivity and functionality of your web pages effortlessly.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy