jQuery | focusin() with Examples

The focusin() is an inbuilt method in jQuery which is used to gain focus on the selected element.
Syntax:

$(selector).focusin(function);

Parameter: It accepts an optional parameter “function” which gain focus on the selected element.
Return Value : It returns the selected element which gain its focus.

jQuery code to show the working of focusin() method:

Code #1:
In the below code, parameter function is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
   
<head>
    <script 
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function() {
            $("div").focusin(function() {
                $(this).css("background-color", "green");
            });
        });
    </script>
    <style>
        div {
            border: 2px solid black;
            width: 50%;
            padding: 20px;
        }
           
        input {
            padding: 5px;
            margin: 10px;
        }
    </style>
</head>
   
<body>
    <!-- click inside the field focusin will take place -->
    <div>
        Enter name:
        <input type="text">
        <br>
    </div>
   
</body>
   
</html>

chevron_right


Output:
Befoe clicking inside of the input field-



After clicking inside of the input field focusin method will be in action-

Code #2:
In the below code, no parameter is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <script 
    </script>
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function() {
            $("#foc").click(function() {
                $(this).focusin().css("background-color", "lightgreen");
            });
        });
    </script>
    <style>
        div {
            border: 2px solid black;
            width: 50%;
            padding: 20px;
        }
          
        input {
            padding: 5px;
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <!-- click inside the field focusin will take place and
    background color becomes change -->
    <div>
        Enter name:
        <input id="foc" type="text">
        <br>
    </div>
  
</body>
  
</html>

chevron_right


Output:
Before clicking inside of the input field-

After clicking inside of the input field focusin method will be in action-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.