Menu Close

JQuery Keydown() Event With Example

Keydown event

In this example, we will learn about the jquery keydown() event. When we press the key on the keyboard, the jquery keydown() event occurs. It triggers the function associated with the event.

The keydown() event is divided into two event methods.

  1. Keypress(): This event specifies that the key is pressed down.
  2. Keyup(): This event specifies that the key is released.
Syntax:$(selector).keydown()  

The above syntax specifies the keydown event for selected elements and it executes the function when it triggers.

Example:

Follow the below example to create a jquery keydown() event.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>JQuery keydown() event with example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> 
</head>
<body>
    Enter your name: <input type="text">
</body>
    <script>
        $(document).ready(function(){  
                $("input").keydown(function(){  
                $("input").css("background-color", "#89CFF0");  
            });  
                $("input").keyup(function(){  
                $("input").css("background-color", "#FBCEB1");  
            });  
        });  
    </script>
</html> 
Posted in HTML, jQuery, Web Technologies

You can also read...