Determine which key was pressed using jQuery

I had a requirement where I need to check which key was pressed by the User in the textbox and then take various action. So below is simple jQuery code to determine or find out which key was pressed. We are using keypress() event of jQuery to detect which key was pressed.

<input type='text' id='txtValue' />
<span id="spn">You have typed: </span>

Below jQuery code first detects the keyCode and then it converts into the character. event.keyCode will give you the ASCII value of the pressed key but if you want to show the entered character then using String.fromCharCode() method, you can get the character.

$(document).ready(function(){
    $('#txtValue').keypress(function(event){
        var txt = $('#spn').text();
      $('#spn').text(txt + String.fromCharCode(event.keyCode));
   });
});
See live Demo and Code.

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...