Skip to content
Related Articles

Related Articles

HTML | DOM onkeypress Event
  • Last Updated : 25 Jul, 2019

The DOM onkeypress event in HTML occurs when a key is pressed by the user.

Order of events related to the onkeypress event:

  • onkeydown
  • onkeypress
  • onkeyup

Supported HTML tags: All HTML elements, EXCEPT:

  • <base>
  • <bdo>
  • <br>
  • <head>
  • <html>
  • <iframe>
  • <meta>
  • <param>
  • <script>
  • <style>
  • <title>


  • In HTML:
    <element onkeypress="myScript">
  • In JavaScript:
    object.onkeypress = function(){myScript};
  • In JavaScript, using the addEventListener() method:
    object.addEventListener("keypress", myScript);

Example: onkeypress Event using the addEventListener() method

<!DOCTYPE html>
      DOM onkeypress event
        <h1 style="color:green">
        <p>Press any key inside 
          the input field</p>
        <input type="text"
              "inputField").addEventListener("keypress", GFGFun);
            function GFGFun() {
                  "inputField").style.backgroundColor = 



Supported Browsers: The browsers supported by HTML DOM onkeypress Event are listed below:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :