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 :first-of-type Selector
Photo Credit to CodeToFun
🙋 Introduction
jQuery is renowned for its ability to simplify web development tasks, offering powerful selectors to target specific elements within HTML documents. One such selector is the :first-of-type
, which enables you to target the first occurrence of a specific type of element effortlessly. Understanding and mastering this selector can significantly enhance your ability to manipulate and interact with HTML elements.
In this comprehensive guide, we'll explore the usage of the jQuery :first-of-type
selector with clear examples to help you grasp its potential.
🧠 Understanding :first-of-type Selector
The :first-of-type
selector allows you to target the first occurrence of a specific type of element within its parent container. This selector is particularly useful when you want to style, manipulate, or perform actions on the first element of its kind without relying on specific IDs or classes.
💡 Syntax
The syntax for the :first-of-type
selector is straightforward:
$("element:first-of-type")
📝 Example
Selecting the First Paragraph:
Suppose you have a container with multiple paragraphs, and you want to select and style the first paragraph. You can achieve this using the
:first-of-type
selector as follows:index.htmlCopied<div id="container"> <p>This is the first paragraph.</p> <p>This is the second paragraph.</p> <p>This is the third paragraph.</p> </div>
example.jsCopied$("#container p:first-of-type").css("font-weight", "bold");
This will make the first paragraph within the container bold.
Applying Styles to the First List Item:
Consider a scenario where you have an unordered list and you want to style the first list item differently. You can utilize the
:first-of-type
selector like so:index.htmlCopied<ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul>
example.jsCopied$("ul li:first-of-type").css("color", "red");
This will change the text color of the first list item to red.
Handling Events on the First Anchor Tag:
You can also bind events to the first occurrence of a specific type of element. For instance, let's attach a click event to the first anchor tag within a container:
index.htmlCopied<div id="container"> <a href="#">First Link</a> <a href="#">Second Link</a> <a href="#">Third Link</a> </div>
example.jsCopied$("#container a:first-of-type").click(function() { alert("You clicked the first link!"); });
Targeting First-of-Type Within Specific Containers:
You can narrow down the scope of the
:first-of-type
selector by specifying a parent container. This is useful when you have multiple instances of the same type of element but only want to target the first one within a particular context.
🎉 Conclusion
The jQuery :first-of-type
selector is a versatile tool for targeting and manipulating the first occurrence of a specific type of element within its parent container. Whether you need to style elements, handle events, or perform other actions on the first element of its kind, this selector provides a convenient solution.
By mastering its usage, you can enhance the interactivity and aesthetics of your web pages with ease.
👨💻 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 :first-of-type Selector), please comment here. I will help you immediately.