6

Enter keyCode(13) works fine on all browsers.

Space bar keyCode(32) I tested on Chrome works fine but not responding on Firefox. I used the following code:

<script type="text/javascript" >
    function enterPressed(evn) {
        var e_id = $('e_id').value;
        var e_fname = $('e_fname').value;
        var e_role = $('e_role').value;

        if (window.event && window.event.keyCode == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn && evn.keyCode == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn && evn.keyCode == 32) {
            Monitor.Order.updateStatus('COOKED');
        }                       
    }
    document.onkeypress = enterPressed;     
</script>

Why is this not working in Firefox when it works in Chrome?

irrelephant
  • 4,091
  • 2
  • 25
  • 41
Fi3n1k
  • 863
  • 3
  • 12
  • 20
  • have you tried with. `evn = evn || window.event` ? – vusan Dec 25 '12 at 10:30
  • Check that: http://stackoverflow.com/questions/7051112/event-which-doesnt-workin-in-firefox – Disa Dec 25 '12 at 10:33
  • No but I don't know how ? – Fi3n1k Dec 25 '12 at 10:33
  • A quick fix would be to use `onkeyup` instead of `onkeypress`, it seems, that FF doesn't trigger `keypress` when space is hit. An example: http://jsfiddle.net/8EFhW/0 – Teemu Dec 25 '12 at 10:46
  • I tried the fiddle works fine and I just tested with simple alert('TEST'); instead of updateStatus() but still not working – Fi3n1k Dec 25 '12 at 10:50
  • Thank you it works with onkeyup. – Fi3n1k Dec 25 '12 at 11:02
  • It seems my explanation why FF fails with this was wrong. `keypress` is fired when hitting `space`, but the `keyCode` is `0` rather than `32`, though `which` returns correct value... – Teemu Dec 25 '12 at 11:50

2 Answers2

5

Space is a printable character, so the keypress event will have the charCode set to the character that it corresponds to and the keyCode won't be set on the keypress event in Firefox.

In general, you want to use charCode for printable things in keypress, keyCode in keyup/keydown.

Boris Zbarsky
  • 34,758
  • 5
  • 52
  • 55
0

try this code it will work fine ....

function enterPressed(evn) {
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x 

        var e_id = $('e_id').value;
        var e_fname = $('e_fname').value;
        var e_role = $('e_role').value;

        if (evn.which == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn.which == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn.which == 32) {
            Monitor.Order.updateStatus('COOKED');
        }                
    }

     else{
            var e_id = $('e_id').value;
            var e_fname =$('e_fname').value;
            var e_role = $('e_role').value;

            if (window.event && window.event.keyCode == 13) {
                Monitor.Order.assign(e_id, e_fname, e_role);
            } else if (evn && evn.keyCode == 13) {
               Monitor.Order.assign(e_id, e_fname, e_role);
            } else if (evn && evn.keyCode == 32) {
                Monitor.Order.updateStatus('COOKED');
            }  

        }
    }
    document.onkeypress = enterPressed;  
Nipun Jain
  • 626
  • 4
  • 6