jquery keypress event object keyCode for firefox problem?

11,644

Solution 1

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

Solution 2

It works for both IE & FF.

 $(document).ready(function (){

         $('#txtEntry').keypress(function (e) {

             $('#lnkValidEdit').focus();
             return false;

         });
Share:
11,644

Related videos on Youtube

Mr Coder
Author by

Mr Coder

Programming != Coding . Trying to start Zend Framework development stack ? use my docker stack a simple LAMP/WAMP/XAMPP replacement . https://github.com/krishjun/zf-docker

Updated on October 16, 2020

Comments

  • Mr Coder
    Mr Coder over 3 years

    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>
    
  • Palak Patel
    Palak Patel almost 8 years
    Worked like a charm... Thanks :)
  • Jasmeen
    Jasmeen about 7 years
    e.charCode || e.keyCode <= This is Perfect :)
  • Mattijs
    Mattijs over 6 years
    Just be aware that according to MDN e.keyCode and e.charCode and e.which are deprecated. I was a bit amazed by that since the replacement, called e.code doesn't have proper support. It seems to only option now is e.key which has IE9 support