jQuery :focus Selector



The :focus selector in jQuery is used to select the element which currently has focus.

Syntax

The syntax is as follows −

$(":focus")

Example

Let us now see an example to implement the :focus() selector −

<!DOCTYPE html> <html> <head> <style>    .one {       color: brown;       background-color: orange;       font-size: 16px;       border: 2px blue solid;    } </style> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script>    $(document).ready(function(){       $("input").focus();       $(":focus").addClass("one");    }); </script> </head> <h2>Fill the form</h2> <body> ID: <input type="text" name="id"><br> Rank: <input type="text" name="rank"><br> Fav. Subjects: Maths: <input type="checkbox" name="sub" value="maths"> English <input type="checkbox" name="sub" value="english"><br> <input type="submit" value="Submit"><br> </body> </html>

Output

This will produce the following output −

Updated on: 2019-11-05T10:12:27+05:30

522 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements