Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery :last-of-type Selector

Posted in jQuery Tutorial
Updated on Oct 30, 2024
By Mari Selvan
👁️ 38 - Views
⏳ 4 mins
💬 1 Comment
jQuery :last-of-type Selector

Photo Credit to CodeToFun

🙋 Introduction

jQuery offers a wide range of selectors to target specific elements within a webpage, allowing developers to manipulate and interact with them dynamically. One such selector is the :last-of-type, which enables you to select the last element of a particular type within its parent. Understanding how to utilize this selector effectively can greatly enhance your ability to style and manipulate elements on your webpage.

In this guide, we'll delve into the usage of the jQuery :last-of-type selector with clear examples to help you grasp its functionality.

🧠 Understanding :last-of-type Selector

The :last-of-type selector is used to target the last element of a specific type within its parent. This can be particularly useful when you want to apply styles or perform actions on the last occurrence of an element, such as the last paragraph or the last list item.

💡 Syntax

The syntax for the :last-of-type selector is straightforward:

syntax.js
Copied
Copy To Clipboard
$("parent_element:last-of-type")

📝 Example

  1. Selecting the Last Paragraph:

    Suppose you have a div containing multiple paragraphs, and you want to select and style the last paragraph differently. You can achieve this using the :last-of-type selector as follows:

    index.html
    Copied
    Copy To Clipboard
    <div id="content">
      <p>First paragraph</p>
      <p>Second paragraph</p>
      <p>Last paragraph</p>
    </div>
    example.js
    Copied
    Copy To Clipboard
    $("#content p:last-of-type").css("font-weight", "bold");

    This will make the last paragraph within the #content div appear bold.

  2. Targeting the Last List Item:

    Consider a scenario where you have an unordered list, and you want to style the last list item differently from the rest. You can use the :last-of-type selector in combination with the list element type (li) to achieve this:

    index.html
    Copied
    Copy To Clipboard
    <ul id="list">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Last item</li>
    </ul>
    example.js
    Copied
    Copy To Clipboard
    $("#list li:last-of-type").css("color", "red");

    This will change the text color of the last list item to red.

  3. Applying Styles to the Last Div:

    You can also target the last occurrence of a specific type of element, such as a div, within its parent container:

    index.html
    Copied
    Copy To Clipboard
    <div class="container">
      <div>First div</div>
      <div>Last div</div>
    </div>
    example.js
    Copied
    Copy To Clipboard
    $(".container div:last-of-type").css("border", "2px solid blue");

    This will add a blue border to the last div within the .container.

🎉 Conclusion

The jQuery :last-of-type selector provides a convenient way to target and manipulate the last occurrence of a specific type of element within its parent. Whether you need to style elements differently, apply actions, or dynamically modify content, this selector offers a flexible solution.

By mastering its usage, you can add finesse and versatility to your web development projects 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
1 Comment
Oldest
Newest Most Voted
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