keydown() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:keydown

Description

The keydown() method triggers the keydown event, or sets function as keydown event handler.

Syntax

Parameter Require Description
function Optional. function to run when the keydown event is triggered

The following code shows how to set the background color of an <input> field when a keyboard key is pressed down:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").keydown(function(){
        $("input").css("background-color", "yellow");
    });//from w w w  .ja  va  2  s .c  o m
    $("input").keyup(function(){
        $("input").css("background-color", "pink");
    });
});
</script>
</head>
<body>

<p>Enter your name.</p>

Enter your name: <input type="text">

</body>
</html>

Related Tutorials