Call method when enter key pressed anywhere in page - Javascript DOM

Javascript examples for DOM:Key Event

Description

Call method when enter key pressed anywhere in page

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/* ww  w.  java2 s . c o  m*/
document.addEventListener("keydown", keyDownTextField, false);
function keyDownTextField(e) {
  var keyCode = e.keyCode;
  if(keyCode==13) {
    console.log("You hit the enter key.");
  } else {
    console.log("Oh no you didn't.");
  }
}
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials