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