Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery keypress event object keyCode for firefox problem?

jQuery keypress event for FireFox gives encrypted keyCode property for event object after String.fromCharCode(e.keyCode) conversion but works perfect in Chrome.

Following is the javascript code:

<!-- #booter and #text are ids of html element textarea -->

<script type="text/javascript">        
    $(function(){
        $('#booter').keypress(function(e){              
            var input = $(this).val() + String.fromCharCode(e.keyCode);
            $('#text').focus().val(input);
            return false;
        });
    });
</script>
like image 853
Mr Coder Avatar asked Jul 29 '11 07:07

Mr Coder


People also ask

What is e keyCode === 13?

Keycode 13 is the Enter key.

What is the difference between keyCode and charCode?

keyCode: Returns the Unicode value of a non-character key in a keypress event or any key in any other type of keyboard event. event. charCode: Returns the Unicode value of a character key pressed during a keypress event.

What is keyCode jQuery?

Introduction to jQuery keycode Key codes are the keyboard keys to which have digital values mapped to the keys based on the key code description. jQuery keycode is a part of Themes in jQuery UI API category, there are many more API's like disableSelection(), enableSelection(), . uniqueId(), . zIndex(), .


1 Answers

You should use e.charCode in Firefox.

$("#booter").keypress(function(e){
     var code = e.charCode || e.keyCode;
     var input = $(this).val() + String.fromCharCode(code);
     $('#text').focus().val(input);
     return false;
});

Try it here:

http://jsfiddle.net/REJ4t/

PS If you're wondering why all this mess: http://www.quirksmode.org/js/keys.html

like image 63
mamoo Avatar answered Oct 15 '22 01:10

mamoo