

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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() with Example
The first() method in jQuery selects the first element from the specified elements.
Syntax
The syntax is as follows −
$(selector).first()
Example
Let us now see an example to implement the jQuery first() method −
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").first().css("color", "blue"); }); </script> </head> <body> <h2>Demo Heading</h2> <p>This is a line. </p> <p>This is another line.</p> <p>This is line 3.</p> </body> </html>
Output
This will produce the following output −
Example
Let us see another example −
<!DOCTYPE html> <html> <head> <style> .one { background-color: orange; border: 2px dashed red; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").first().addClass("one"); }); </script> </head> <body> <h2>Demo Heading</h2> <p>This is a line. </p> <p>This is another line.</p> <p>This is line 3.</p> <p>This is line 4.</p> <p>This is line 5.</p> </body> </html>
Output
This will produce the following output −
- Related Questions & Answers
- jQuery focusin() with Example
- jQuery finish() with Example
- jQuery addClass() with Example
- jQuery focusout() with Example
- jQuery nextUntil() with Example
- jQuery appendTo() with Example
- jQuery closest() with Example
- jQuery dblclick() with Example
- jQuery fadeOut() with Example
- jQuery find() with Example
- jQuery focus() with Example
- jQuery click() with Example
- jQuery blur() with Example
- jQuery innerWidth() with Example
- jQuery change() with Example
Advertisements