jQuery Topics
- jQuery Introduction
- jQuery Callbacks
- jQuery deferred
- jQuery selectors
- *
- :animated
- [name|=”value”]
- [name*=”value”]
- [name~=”value”]
- [name$=”value”]
- [name=”value”]
- [name!=”value”]
- [name^=”value”]
- :button
- :checkbox
- :checked
- Child Selector
- .class
- :contains()
- Descendant Selector
- :disabled
- Element
- :empty
- :enabled
- :eq()
- :even
- :file
- :first-child
- :first-of-type
- :first
- :focus
- :gt()
- Has Attribute
- :has()
- :header
- :hidden
- #id
- :image
- :input
- :lang()
- :last-child
- :last-of-type
- :last
- :lt()
- [name=”value”][name2=”value2″]
- (“selector1, selector2, selectorN”)
- (“prev + next”)
- (“prev ~ siblings”)
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :odd
- :only-child
- :only-of-type
- :parent
- :password
- :radio
- :reset
- :root
- :selected
- :submit
- :target
- :text
- :visible
- jQuery Ajax Events
- jQuery Ajax Methods
- jQuery Keyboard Events
- jQuery Keyboard Methods
- jQuery Form Events
- jQuery Form Methods
- jQuery Mouse Events
- jQuery Mouse Methods
- jQuery Event Properties
- jQuery Event Methods
- jQuery HTML
- jQuery CSS
- jQuery Fading
- jQuery Traversing
- jQuery Utilities
- jQuery Properties
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:
$("parent_selector :nth-last-child(n)")
📝 Example
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.htmlCopied<div class="parent"> <p>First child</p> <p>Second child</p> <p>Last child</p> </div>
example.jsCopied$(".parent :nth-last-child(1)").css("font-weight", "bold");
This will make the last child element within the .parent container bold.
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.htmlCopied<div class="parent"> <p>First paragraph</p> <p>Second paragraph</p> <p>Third paragraph</p> <p>Fourth paragraph</p> </div>
example.jsCopied$(".parent :nth-last-child(2)").css("color", "blue");
This will change the color of the second-to-last paragraph element to blue.
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.htmlCopied<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.jsCopied$(".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.
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:
Author
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
If you have any doubts regarding this article (jQuery :nth-last-child() Selector), please comment here. I will help you immediately.