Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery :nth-last-child() Selector

Posted in jQuery Tutorial
Updated on Apr 28, 2024
By Mari Selvan
👁️ 7 - Views
⏳ 4 mins
💬 0
jQuery :nth-last-child() Selector

Photo Credit to CodeToFun

🙋 Introduction

In jQuery, the :nth-last-child() selector is a powerful tool for targeting elements based on their position within their parent container, counting from the end. This selector allows you to select elements dynamically, enabling you to apply styles, manipulate content, or perform actions based on their position in the DOM tree.

In this guide, we'll explore the usage of the jQuery :nth-last-child() selector with clear examples to help you grasp its functionality.

🧠 Understanding :nth-last-child() Selector

The :nth-last-child() selector targets elements based on their position relative to their parent container, counting from the last child element. This is particularly useful when you want to select elements dynamically without knowing their exact position in the document structure.

💡 Syntax

The syntax for the :nth-last-child() selector is straightforward:

syntax.js
Copied
Copy To Clipboard
$("parent_selector :nth-last-child(n)")

📝 Example

  1. Selecting the Last Child Element:

    If you want to select the last child element of a parent container, you can use the :nth-last-child(1) selector as follows:

    index.html
    Copied
    Copy To Clipboard
    <div class="parent">
      <p>First child</p>
      <p>Second child</p>
      <p>Last child</p>
    </div>
    example.js
    Copied
    Copy To Clipboard
    $(".parent :nth-last-child(1)").css("font-weight", "bold");

    This will make the last child element within the .parent container bold.

  2. Selecting Every Second-to-Last Element:

    You can target elements based on their position relative to the last child using the :nth-last-child() selector. For example, to select every second-to-last paragraph element within a container:

    index.html
    Copied
    Copy To Clipboard
    <div class="parent">
      <p>First paragraph</p>
      <p>Second paragraph</p>
      <p>Third paragraph</p>
      <p>Fourth paragraph</p>
    </div>
    example.js
    Copied
    Copy To Clipboard
    $(".parent :nth-last-child(2)").css("color", "blue");

    This will change the color of the second-to-last paragraph element to blue.

  3. Applying Styles to Every Third-to-Last Element:

    You can also apply styles to elements counting from the end. For instance, to style every third-to-last list item within a container:

    index.html
    Copied
    Copy To Clipboard
    <ul class="parent">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
    </ul>
    example.js
    Copied
    Copy To Clipboard
    $(".parent li:nth-last-child(3)").css("background-color", "lightgray");

    This will set the background color of the third-to-last list item to light gray.

  4. Targeting Elements Based on Dynamic Position:

    The :nth-last-child() selector allows you to target elements dynamically, making it versatile for various scenarios. Experiment with different values of n to select elements based on their position from the end.

🎉 Conclusion

The jQuery :nth-last-child() selector provides a flexible way to target elements based on their position relative to the last child within a container. Whether you need to select the last element, target specific elements counting from the end, or apply styles dynamically, this selector offers powerful capabilities.

By mastering its usage, you can enhance your web development projects with dynamic and responsive designs.

👨‍💻 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