Open In App

How to run the code on keypress event using jQuery ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to run the code snippet on the keypress event using jQuery. The keypress event is triggered when the keyboard button is pressed by the user.

Syntax:

$(selector).keypress()

In the below example, we are creating a textarea element and when the user starts pressing a key to write some content on the textarea, the keypress event is triggered. When this event is triggered, the CSS styles are added to the element. 

Example: In this example, we will see the use of a keypress event.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to run the code on keypress
        event using jQuery ?
    </title>
    <script src=
    </script>
    <script>
        $(document).ready(function () {
            $("textarea").keypress(function () {
                $("textarea").css({
                    background: "green",
                    color: "white",
                });
            });
        });
    </script>
</head>
 
<body>
    <center>
        <h1 style="color: green">GeeksforGeeks</h1>
        <h3>
            How to run the code on keypress
            event using jQuery ?
        </h3>
        <textarea rows="5" cols="30"></textarea>
    </center>
</body>
 
</html>


Output: 



Last Updated : 30 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads