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-child Selector
Photo Credit to CodeToFun
🙋 Introduction
jQuery offers a plethora of selectors to simplify DOM manipulation, and one of the most useful ones is the :first-child
selector. This selector enables you to target the first child element of its parent effortlessly. Understanding how to utilize the :first-child
selector effectively can streamline your web development process and enhance the interactivity of your web pages.
In this guide, we'll explore the usage of the jQuery :first-child
selector through clear examples to help you grasp its full potential.
🧠 Understanding :first-child Selector
The :first-child
selector targets the first child element within its parent. It's particularly handy when you need to apply styles or perform actions specifically on the first child of an element.
💡 Syntax
The syntax for the :first-child
selector is straightforward:
$("parentSelector :first-child")
📝 Example
Selecting the First Paragraph Element:
Suppose you have a <div> containing multiple <p> elements, and you want to target the first <p> element for styling. You can achieve this using the
:first-child
selector as follows:index.htmlCopied<div id="container"> <p>First paragraph</p> <p>Second paragraph</p> <p>Third paragraph</p> </div>
example.jsCopied$("#container p:first-child").css("font-weight", "bold");
This will make the text of the first paragraph bold.
Applying Styles to the First List Item:
Consider a <ul> list where you want to emphasize the appearance of the first list item. You can use the
:first-child
selector to achieve this effect:index.htmlCopied<ul id="list"> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul>
example.jsCopied$("#list li:first-child").css("color", "blue");
This will change the color of the text in the first list item to blue.
Adding a Class to the First Table Row:
Suppose you have a table and you want to differentiate the styling of the first row. You can utilize the
:first-child
selector to add a class to the first <tr> element:index.htmlCopied<table id="table"> <tr><td>Row 1</td></tr> <tr><td>Row 2</td></tr> <tr><td>Row 3</td></tr> </table>
example.jsCopied$("#table tr:first-child").addClass("highlighted");
This will add a class named "highlighted" to the first table row, allowing you to apply specific styles.
Chaining Selectors with :first-child:
You can combine the
:first-child
selector with other selectors to target more specific elements. For instance, $("#container p:first-child") selects the first paragraph element within the container, ensuring precise targeting.
🎉 Conclusion
The jQuery :first-child
selector is a powerful tool for targeting and manipulating the first child element within its parent. Whether you need to style elements, apply classes, or perform other actions specific to the first child, this selector provides a straightforward solution.
By mastering its usage, you can enhance the visual appeal and functionality 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-child Selector), please comment here. I will help you immediately.