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