Skip to content
Related Articles
Open in App
Not now

Related Articles

jQuery focusin() Method

Improve Article
Save Article
  • Last Updated : 18 Nov, 2022
Improve Article
Save Article

The jQuery focusin() is an inbuilt method 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. 

jQuery examples to show the working of focusin() method: 
Example 1: In the below code, parameter function is passed. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </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>

Output: 

 

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

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </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>

Output: 

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!