-->

jQuery keydown() method

By: Bipin C Mathew 6 months, 3 weeks ago

The order of events related to the keydown event:

keydown - The key is on its way down

The keydown event occurs when a keyboard key is pressed down.

The keydown() method triggers the keydown event, or attaches a function to run when a keydown event occurs.

Syntax:

$(selector).keydown() - Trigger the keydown event for the selected elements. 

$(selector).keydown(function) - Attach a function to the keydown event. 

Example:

<!doctype html>
	<html lang="en">
		<head>
		<meta charset="utf-8">

		<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
		<script>  
		$(document).ready(function(){  
		    $("input").keydown(function(){  
				$("input").css("background-color", "green");  
		    });  
		    $("input").keyup(function(){  
				$("input").css("background-color", "violet");  
		    });  
		});  
		</script>   
		</head>
		<body>
			Write Anything: <input type="text">  
		</body>
	</html>

Keyup occurs when we release a key and Keydown occurs when we press a key on keyboard. 

Comments

Let us talk!

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.