Forum Moderators: open
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
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
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