11

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>
Alex R.
  • 4,664
  • 4
  • 30
  • 40
Mr Coder
  • 8,169
  • 5
  • 45
  • 74

2 Answers2

20

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

mamoo
  • 8,156
  • 2
  • 28
  • 38
  • Worked like a charm... Thanks :) – Palak Patel Jun 30 '16 at 06:04
  • 1
    e.charCode || e.keyCode <= This is Perfect :) – Jasmeen Apr 07 '17 at 07:03
  • 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 – Mattijs Aug 25 '17 at 02:05
1

It works for both IE & FF.

 $(document).ready(function (){

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

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

         });
Mohsin Razuan
  • 11
  • 1
  • 1