Dispatching a key event with preventDefault - Javascript DOM

Javascript examples for DOM:Key Event

Description

Dispatching a key event with preventDefault

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() {/*from   w  w  w.java2  s .co m*/
document.getElementById("foo").addEventListener("keypress", function(event) {
    if (event.keyCode === 65) {
      document.getElementById("foo").value += String.fromCharCode(1740);
      event.preventDefault();
    }
}, false);
    });

      </script> 
   </head> 
   <body> 
      <input id="foo" value="">  
   </body>
</html>

Related Tutorials