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 :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:
$("parent_element:last-of-type")
📝 Example
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.htmlCopied<div id="content"> <p>First paragraph</p> <p>Second paragraph</p> <p>Last paragraph</p> </div>
example.jsCopied$("#content p:last-of-type").css("font-weight", "bold");
This will make the last paragraph within the #content div appear bold.
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.htmlCopied<ul id="list"> <li>Item 1</li> <li>Item 2</li> <li>Last item</li> </ul>
example.jsCopied$("#list li:last-of-type").css("color", "red");
This will change the text color of the last list item to red.
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.htmlCopied<div class="container"> <div>First div</div> <div>Last div</div> </div>
example.jsCopied$(".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:
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 :last-of-type Selector), please comment here. I will help you immediately.