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 :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.