HTML | onkeypress Attribute

This attribute fires when a user presses a key on the Keyboard. This event attribute can not be used for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers.

Syntax:

<element onkeypress="script">

Attribute: The onkeydown attribute works for all keys in all browsers. The script will be run when onkeypress attribute call.



Note: The order of events that is related to the onkeypress events:

  • onkeydown
  • onkeypress
  • onkeyup

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>onkeypress attribute</title>
        <style>
            body {
                text-align:center;
            }    
            h1 {
                color:green;
            }
        </style>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>onkeypress attribute</h2>
        <input type="text" onkeypress="GeeksForGeeks()">
        <script>
            function GeeksForGeeks() {
                alert("Welcome to GeeksForGeeks!");
            }
        </script>
    </body>
</html>                    

chevron_right


Output:
Before click in the text box:

After click on the text box:

Supported Browsers: The browser supported by onkeypress event attribute are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Apple Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.