
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
jQuery :first-of-type Selector
The :first-of-type selector in jQuery is used to select elements, which are the first element of their parent.
Syntax
The syntax is as follows −
$(":first-of-type")
Example
Let us now see an example to implement the :first-of-type() selector −
<!DOCTYPE html> <html> <head> <style> .demo { background-color: red; color: white; font-size: 16px; border: 2px blue dashed; } </style> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script> $(document).ready(function(){ $("p:first-of-type").addClass("demo"); }); </script> </head> <body> <p>This is the first line!</p> <p>This is the second line!</p> <p>This is the third line!</p> <hr> <div> <span>One</span> <p>Two</p> <span>Three</span> <p>Four</p> </div> <hr> <div> <p>A</p> <p>B</p> </div> <hr> <p>This is the last line.</p> </body> </html>
Output
This will produce the following output −
Advertisements