Welcome to WebmasterWorld Guest from 54.145.58.37

Forum Moderators: open

Message Too Old, No Replies

event.keycode Firefox

event.keycode does not work in Firefox

     
7:14 am on Oct 27, 2011 (gmt 0)

New User

5+ Year Member

joined:May 12, 2009
posts: 21
votes: 0


Now I've been banging my head against the wall with this for a while now.. say I have this script:

<script type="text/javascript">
document.onkeyup = KeyCheck;
function KeyCheck()
{
var KeyID = event.keyCode;
switch(KeyID)
{
case 13:
alert("hello");
}
}
</script>


The code should make an alert box with the text "hello" when someone presses the Enter key.
Why doesn't this work in Firefox? (tested in Chrome, Opera and IE, all work fine)
10:19 am on Oct 27, 2011 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts:970
votes: 23


Hi there jaruba,

try it like this...

<script type="text/javascript">
document.onkeyup=KeyCheck;
function KeyCheck(evt) {
var evt=(evt)?evt:event;
var KeyID = evt.keyCode;
switch(KeyID) {
case 13:
alert("hello");
break;
default:
alert("not 13");
}
}
</script>

birdbrain
10:27 am on Oct 27, 2011 (gmt 0)

New User

5+ Year Member

joined:May 12, 2009
posts: 21
votes: 0


You, my friend, are a titan among humble humans.. :))

It works as expected in IE, Firefox, Opera and Chrome. Thank you.
1:06 pm on Oct 27, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4988
votes: 12


The reason, fyi, is that event handling is done differently in IE than other browsers. IE has a single global event object, while other browsers pass the event object to the handler.
2:43 pm on Oct 27, 2011 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 970
votes: 23


Hi here jaruba,

You, my friend, are a titan among humble humans...

That is really weird. :)

I awoke this morning as just another "bald headed old fart", then typed
a few characters on my keyboard and became a Greek god before lunch.

birdbrain
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members