jQuery focusin() with Example


The focusin() method in jQuery occurs when an element gets focus.

Syntax

The syntax is as follows −

$(selector).focusin(function)

Example

Let us now see an example to implement the jQuery focusin() method −

<!DOCTYPE html>
<html>
<head>
<style>
   .demo {
      color: blue;
   }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $("input").focusin(function(){
      $("p").html("focusin event triggered");
   });
   $("input").focusout(function(){
      $("p").html("focusout event triggered");
   });
});
</script>
</head>
<body>
<h2>Student Details</h2>
Student Name: <input type="text"><br>
<br><br>
<p class="demo"></p>
</body>
</html>

Output

This will produce the following output −

Now, click inside to get focus −

Click outside to lose focus −

Updated on: 12-Nov-2019

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements