jQuery keydown()

The jQuery keydown() method is used to attach a function to run when a keydown event occurs i.e, when a key is pressed on the keyboard.

Syntax:

To trigger the keydown event for selected elements.

$(selector).keydown() 

To add a function to the keydown event.

$(selector).keydown(function)  

Function:

  • It is an optional parameter.
  • The function parameter specifies the function to run when the event occurs.

 

Example 1

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){  
	$("input").keydown(function(){  
    	$("input").css("background-color", "turquoise");  
	});  
	$("input").keyup(function(){  
    	$("input").css("background-color", "red");  
	});  
});  
</script>  
</head>  
<body>  
Write something: <input type="text">  
</body>  
</html>

Try it

JS Bin on jsbin.com

Please Share