jQuery Keyboard Events Complete Guide

Updated on ... 17th February 2023

jQuery Keyboard Events refer to the set of events that are triggered by the keyboard keys. With jQuery, you can bind or attach various keyboard events to HTML elements and perform specific actions when the user interacts with them using the keyboard. Some of the most commonly used keyboard events in jQuery include keypress, keydown, and keyup. These events can be used to capture the user's input, validate data, control the flow of the application, and provide a better user experience.

However, jQuery provides several keyboard events that allow you to handle user input via the keyboard. Here are some of the most commonly used keyboard events in jQuery:

  1. .keydown(): The keydown() event is triggered when a key on the keyboard is pressed down.
  2. .keyup(): The keyup() event is triggered when a key on the keyboard is released.
  3. .keypress(): The keypress() event is triggered when a key that produces a character value is pressed down. This event is similar to keydown(), but it is not triggered for keys that do not produce a character value (such as the arrow keys).

To use these events, you can attach them to an element using the jQuery on() method. For example, to handle the keydown() event on a text input element with the ID "myInput", you could write the following code:

To use these events, you can attach them to an element using the jQuery on() method. For example, to handle the keydown() event on a text input element with the ID "myInput", you could write the following code:

                                                    

                                                    

                                                    $('#myInput').on('keydown', function(event) {
  console.log('Key pressed:', event.which);
});

                                                    

                                                

Jquery keydown() event

The keydown() event in jQuery is triggered when a keyboard key is pressed down. This event is attached to an element and is fired whenever a user presses a key while the element is in focus.

The syntax to bind the keydown() event in jQuery is as follows:


                                                    

                                                    

                                                    $(selector).keydown(function() {
  // code to be executed when the key is pressed down
});

                                                    

                                                

You Should Also Read

Here, the $(selector) represents the HTML element to which the event is attached, and the anonymous function specifies the code to be executed when the event is triggered.

The keydown() event is often used to capture the user's input and perform specific actions based on the input. For example, you can use the keydown() event to validate form inputs, control the flow of the application, or provide shortcuts to the user.

Here's an example that demonstrates how to use the keydown() event to toggle the background color of an element when the user presses the 'b' key:

                                                    

                                                    

                                                    <!DOCTYPE html>
<html>
<head>
  <title>jQuery keydown() event example</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <style>
    .highlight {
      background-color: yellow;
    }
  </style>
</head>
<body>
  <p>Press the 'b' key to toggle the background color</p>
  <div id="mydiv">This is a div element</div>
  <script>
    $(document).keydown(function(event) {
      if (event.key === 'b') {
        $('#mydiv').toggleClass('highlight');
      }
    });
  </script>
</body>
</html>

                                                    

                                                

jQuery keyup() event

The jQuery keyup() event is triggered when a keyboard key is released after being pressed. It is a type of keyboard event that is used to detect when a user releases a key on the keyboard.

The syntax for using the keyup() method is as follows:

                                                    

                                                    

                                                    $(selector).keyup(function() {
  // code to be executed
});

                                                    

                                                
In the above syntax, the selector refers to the HTML element(s) that the keyup() event that will be attached. The function that is passed as an argument to the keyup() method will be executed whenever the keyup event is triggered.

Here is an example of using the keyup() event to detect when the "Enter" key is released:

                                                    

                                                    

                                                    <!DOCTYPE html>
<html>

<head>
    <title>jQuery keydown() event example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .mydiv {
            font-family: sans-serif;
            padding: 40px;
        }
    </style>
</head>

<body>
    <div id="mydiv">
        <p>Enter any key and you will see the result</p>
        <input type="text" name="name" id="name"><br>
        <span id="name_val"></span>
    </div>
    <script>
        $(document).ready(function () {
            $('#name').keyup(function (event) {
                $('#name_val').html("You Enter:<b>    " + event.key + "<b>");
            });
        });
    </script>
</body>

</html>

                                                    

                                                

In the above example, the keyup() event is attached to the entire document object. When a key is released, the function is executed and checks whether the released key is the "Enter" key. If it is, the code inside the if block will be executed.

The keyup() event can be used to detect when any key on the keyboard is released. It is often used in combination with other keyboard events, such as keydown() and keypress(), to create complex interactions with keyboard input.

jQuery keypress() event 

The jQuery keypress() event is triggered when a keyboard key is pressed down and released. It is a type of keyboard event that is used to detect when a user presses a key on the keyboard.

The syntax for using the keypress() method is as follows:


                                                    

                                                    

                                                    $(selector).keypress(function() {
  // code to be executed
});

                                                    

                                                

In the above syntax, the selector refers to the HTML element(s) that the keypress() event will be attached to. The function that is passed as an argument to the keypress() method will be executed whenever the keypress event is triggered.

Here is an example of using the keypress() event to detect when a specific key is pressed:


                                                    

                                                    

                                                    <!DOCTYPE html>
<html>

<head>
    <title>jQuery keypress() event example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .mydiv {
            font-family: sans-serif;
            padding: 40px;
        }
    </style>
</head>

<body>
    <div id="mydiv">
        <p>Press a from your keyboard for seeing alert message</p>
    </div>
    <script>
        $(document).keypress(function (event) {
            if (event.key === "a") {
                alert("you presses a from your keyboard");
            }
        });
    </script>
</body>

</html>

                                                    

                                                

Related Post

Leave a comment