Skip to content
Related Articles

Related Articles

Improve Article

HTML | DOM onkeydown Event

  • Last Updated : 25 Jul, 2019
Geek Week

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

Order of events related to the onkeydown 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 onkeydown="myScript">>
  • In JavaScript:
    object.onkeydown = function(){myScript};
  • In JavaScript, using the addEventListener() method:
    object.addEventListener("keydown", myScript);

Example: onkeydown Event using the addEventListener() method

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



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

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

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :