Home >>jQuery Tutorial >jQuery keydown() Method

jQuery keydown() Method

jQuery keydown() Method

jQuery keydown() method in jQuery is used to add a function or triggers the keydown event to run when a keydown event occurs.

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

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

Parameter Values

Parameter Description
function It is optional parameter and is used to specifies the function to run when the keydown event is triggered
Here is an Example of jQuery keydown() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").keydown(function(){
$("input").css("background-color", "orange");
});
$("input").keyup(function(){
$("input").css("background-color", "blue");
});
});
</script>
</head>
<body>
Username: <input type="text">
<p> It will change background color on keydown and keyup when enter text in the above input field.</p>
</body>
</html>

Output:
Username:

It will change background color on keydown and keyup when enter text in the above input field.