Keypress event in javascript

We will use Keypress event to execute one or more JavaScript function when a user press a key.
The "Keypress" event will be fired after the "keydown" and before the "keyup" events.


Example:

In the below example when we pass the "event" as parameter to the function, the keypress event will passed as a parameter and when we pass "this" as parameter, then the event calling control will be passed as parameter.

Enter Value :




Code Sample:

<html> <head> <title>Keypress event example in JavaScript</title> <script language="Javascript"> var res = ""; function keydown1(evt, t) { res = "keydown- keycode:" + evt.keyCode + ", value:" + t.value + "<br/>"; document.getElementById("tdresult").innerHTML = res; } function keypress1(evt, t) { if (window.event) res = res + "keypress- keycode:" + evt.keyCode + ", value:" + t.value + "<br/>"; else res = res + "keypress- keycode:" + evt.charCode + ", value:" + t.value + "<br/>"; document.getElementById("tdresult").innerHTML = res; } </script> </head> <body> <table align="left"> <tr> <td> Enter Value : <input type="text" id="Text1" onkeydown="keydown1(event,this);" onkeypress="keypress1(event,this);" style="width: 200px;" /> </td> </tr> <tr> <td id="tdresult"></td> </tr> </table> </body> </html>