Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space bar keyCode(32) not working on Mozilla Firefox

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?

like image 484
Fi3n1k Avatar asked Dec 25 '12 10:12

Fi3n1k


People also ask

Is keyCode deprecated?

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.

What is e keycode === 13?

key 13 keycode is for ENTER key.

What is the keycode for spacebar in JavaScript?

JavaScript keycode of the space bar is 32.


1 Answers

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.

like image 87
Boris Zbarsky Avatar answered Oct 12 '22 12:10

Boris Zbarsky