Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




EVENTS - focus()


The focus() Event gets executed, when a field (Like an text or textarea field) gets focus.


Example :



<html>
  	<head>
    	<title> My First Programme </title>
  	</head>

  	<body>
    	<h1> JQuery </h1>
    		
    	Type Something : <input type = "text"> 		

      	<script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script>
	  
      	<script>
      
			$('input').focus( function() { 
            	$(this).css("background-color", "yellow")
            });
            
      	</script>      
  	</body>
</html> 


Output :



So, if you look at the above code, we have an <input> element, that accepts a text.


<input type = "text">

Now, if we take a look at the JQuery statement,


$('input').focus( function() {
	$(this).css("background-color", "yellow")
});

At first we locate the <input> element,


$('input')

Then we use the focus() event,


$('img').focus(...)

And put the function inside focus() event to change the color of <input> element.


$('input').focusin( function() {
	$(this).css("background-color", "yellow")
});

And the function,


function() {
	$(this).css("background-color", "yellow")
});

Locates the current element (i.e. <input>) using this and changes the color of the <input> element.


Note : In simple words, when we click on the <input> element, it gets the focus.