When you press a key on the keyboard, the keydown() event is occurred and once the keydown() event is occurred, it executes the function associated with keydown() method to run.
The keydown() event is generally used with two other events.
Syntax:
$(selector).keydown()
It triggers the keydown event for selected elements.
$(selector).keydown(function)
It adds a function to the keydown event.
Parameter | Description |
---|---|
Function | It is an optional parameter. It is executed itself when the keydown event is triggered. |
Let's take an example to demonstrate jQuery keydown() event.
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-1.10.2.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 something: <input type="text"> </body> </html>
Output: