focus() method

focus() method
The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it).The focus() method triggers the focus event or attaches a function to run when a focus event occurs.


Syntex:
$(selector).focus()



Example:-


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").focus(function(){
        $("span").css("display", "inline").fadeOut(2500);
    });
});
</script>
<style>
span {
    display: none;
}
</style>
</head>
<body>

<input>

<span>Nice to meet you Mr. Nitin chauhan !</span>
<p>Click inside the input field to get focus.</p>

</body>
</html>

Example :2-
<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="utf-8"> 
  <title>focus() demo</title> 
  <style> 
  span { 
    display: none; 
  } 
  </style> 
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
</head> 
<body> 
 <p><input type="text"> <span>Focus starts.. Write something inside input field 1.</span></p> 
<p><input type="password"> <span>Focus starts.. Write something inside input field 2.</span></p> 
 <script> 
$( "input" ).focus(function() { 
  $( this ).next( "span" ).css( "display", "inline" ).fadeOut( 2000 ); 
}); 
</script> 
 </body> 
</html>