mcbeav
mcbeav

Reputation: 12275

Keypress not firing In Firefox

I am using a JavaScript keypress switch to fire events, and it works fine in webkit browsers, but it does not work in Firefox. Can anyone help? The code I am using is:

$(document).keydown(function(e) {
switch(e.keyCode) { 

    case 39:
    event.preventDefault();
            alert("Arrow Key");
 }
break;

    case 37:
           event.preventDefault();
            alert("Arrow Key");
}
});

The functions I am trying to fire are more complex than just an alert, but i thought i would keep it simple for the explanation.

Upvotes: 1

Views: 1609

Answers (2)

Dr.Molle
Dr.Molle

Reputation: 117314

You have an syntax-error(a wrong bracket } before break;), and an undefined object(event) inside your function.

$(document).keydown(function(e) {
switch(e.keyCode) { 

case 39:
e.preventDefault();
        alert("Arrow Key");

break;

case 37:
       e.preventDefault();
        alert("Arrow Key");
}
});

The wrong object(event) does'nt occur in MSIE, as there is always a global object called "event"

Upvotes: 2

Sébastien VINCENT
Sébastien VINCENT

Reputation: 1106

IIRC Firefox use charCode and not keyCode.

Can you try that :

$(document).keydown(function(e) {
kCode = (e.keyCode)? e.keyCode: e.charCode;
switch(kCode) { 

case 39:
event.preventDefault();
        alert("Arrow Key");
}
break;

case 37:
       event.preventDefault();
        alert("Arrow Key");
}
});

Upvotes: 3

Related Questions