3

I want to trigger the onkeydown event on the html page to do some actions. I have the following code:

document.onkeydown = function(){
    alert('dd');
};

Is it possible to trigger this event on whole page or is it limited to only editable html elements?

hugomg
  • 68,213
  • 24
  • 160
  • 246
Suraj
  • 43
  • 1
  • 6

3 Answers3

1

You can just call

document.onkeydown();
Krzysztof
  • 15,900
  • 2
  • 46
  • 76
  • Note that this only works if the event was added by setting `document.onkeydown = ` directly. If you used addEventListener things are different – hugomg Nov 26 '11 at 14:47
0

You can trigger event on body tag

    <html>
        <head>
        <script type="text/javascript" language="JavaScript">

        function changeVal() {
          alert('d');
        } 

        </script>
        </head>

        <body onKeyDown="changeVal();">
        <form action="" method="POST" id="myForm">

        </form>
        </body>
  </html>
pramodtech
  • 6,300
  • 18
  • 72
  • 111
0

On page: https://developer.mozilla.org/en/DOM/window.onkeydown

On an element: https://developer.mozilla.org/en/DOM/element.onkeydown

Determine which key was pressed: https://developer.mozilla.org/en/DOM/event.which

Kai
  • 9,038
  • 5
  • 28
  • 28