Forum Moderators: open

Message Too Old, No Replies

onKeyUp Help

can't get it to work in IE

         

humpg

6:58 pm on May 10, 2006 (gmt 0)

10+ Year Member



Hey Everybody,

Not very experienced with javascript but I am trying to detect a keypress. In this particular instance I am trying to detect the 'Enter' Button. It works fine in netscape, firefox, safari but I can't get it to work in IE (cross browser thing i bet).

here is my code:

var start = 0;
var keyboardCode;

window.captureEvents(Event.KEYUP);
window.onkeyup = submitAnswers;

function submitAnswers(e) {

keyboardCode = e.keyCode;

if ((keyboardCode = 13) && (start == 0))
{document.getElementById('enterToStart').style.visibility = 'hidden';}

}

I don't want this to be associated with any textbox, textarea, etc. I want it to work right as soon as the page loads. So if they hit enter my div will dissappear. I'm sure this is easy to solve. So thanks in advance.

Thanks,

Gary

jshanman

7:26 pm on May 10, 2006 (gmt 0)

10+ Year Member



function checkKey(e) {
if (!e) var e = window.event;
if (e.keyCode) {
code = e.keyCode;
} else if (e.which) {
code = e.which;
}
if (code == 13 && start == 0) document.getElementById('enterToStart').style.visibility = 'hidden';
}

window.onkeyup = checkKey;

- JS

humpg

11:53 am on May 11, 2006 (gmt 0)

10+ Year Member



Nothing happens still? but still works in the other browsers.

Any Idea?

jshanman

12:40 pm on May 11, 2006 (gmt 0)

10+ Year Member



[quote[
Nothing happens still? but still works in the other browsers.

Any Idea? [/quote]

Nothing happens!? Do you at least get an error message? Try putting an alert in the function to see what the value of *code* is, and if the script even makes it to the function.

- JS

humpg

1:34 pm on May 11, 2006 (gmt 0)

10+ Year Member



I put an alert in, and it's not even making it into the function. But just in IE.

humpg

3:12 pm on May 11, 2006 (gmt 0)

10+ Year Member



I can get it to work in IE if I add this to the body tag:

<body onKeyPress="checkKey();">

BUT, then it doesn't work in firefox.

p.s - I changed it to keypress instead of keyup

jshanman

6:06 pm on May 11, 2006 (gmt 0)

10+ Year Member



I can get it to work in IE if I add this to the body tag:

<body onKeyPress="checkKey();">

BUT, then it doesn't work in firefox.

So do it both ways! IIRC it has something to do with an object needing focus for the keypress event to be fired.

try:

document.body.onkeypress = checkKey;

- JS

humpg

12:13 pm on May 12, 2006 (gmt 0)

10+ Year Member



GOT IT,

You were close, but what I needed to write was:

document.onkeypress = checkKey;

I came across it when looking for some more solutions on google. Then reading further I found that this is the proper way to handle these events:

document.addEventListener("keypress",checkKey,true);

Still doing some reading to understand it completely but I got the just of it.

So thanks jshanman you have been a great help in sending me in the right direction, I appreciate and will be moving on with my app. And will probably be back soon with more questions.

LOL

jshanman

2:24 pm on May 12, 2006 (gmt 0)

10+ Year Member



Just be aware:

document.addEventListener("keypress",checkKey,true);

may not work with ie very well, try this:
(from [developer.mozilla.org...]

if (document.addEventListener){
document.addEventListener('keypress', checkKey, false);
} else if (document.attachEvent){
document.attachEvent('onkeypress', checkKey);
}

- JS

humpg

6:23 pm on May 12, 2006 (gmt 0)

10+ Year Member



Yup,

I caught that after I posted my reply, LOL. Took what you said and it works great, Thanks again.

Gary