Click to See Complete Forum and Search --> : Getting keycode from onKeyPress on a hyperlink


Alsvha
October 3rd, 2005, 08:51 AM
Hiya all.

Suppose I have a hyperlink. If this gets focus and I press enter, I'd like to capture that enter event and do some stuff, this I'd think would be simple ... but nooooo :D

I have a link where I've set the onKeyPress event on which calls a function (simplified):

function trapEnterNote(intID, e)
{
var keycode;
if (window.event) keycode = window.event.keycode;
else if (e) keycode = e.which;
else return true
alert(keycode);
return false;
}

This function gets trigger well enough - which means that the onKeyPress works. However - when I try to alert the keycode I get "undefined" in IE and "0" in FireFox.

The excat same code works with input=text fields which leads me to belive that I can't get the keycode from onKeyPressed used on a hyperlink at all.
Is this a correct assumption or am I doing something wrong?

Regards.

Alsvha
October 3rd, 2005, 09:27 AM
Memo to self: remember to learn to the difference between capital letters and non-capital :D

Problem solved, it was the keyCode instead of keycode :o

PeejAvery
October 3rd, 2005, 09:29 AM
EDIT: I guess we posted at the exact time. I didn't see you second post until after I posted this.


Does this help?

<html>
<body onkeydown="checkkey(event)">

<script language="JavaScript">
function checkkey(e){
window.alert(e.keyCode);
if(e.keyCode==keycode_number_goes_here){
//put what you want here...
}
}
</script>


</body>
</html>