jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
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.