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




EVENTS - keydown()


The keydown() Event gets executed, when a key (Any key in the keyboard) is pressed.


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').keydown( 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').keydown( function() {
	$(this).css("background-color", "yellow")
});

At first we locate the <input> element,


$('input')

Then we use the keydown() event,


$('input').keydown(...)

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


$('input').keydown( 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, the moment we start typing something using the keyboard, 'keydown()' event is executed.