By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

Keypress on a hyperlink

P: n/a
Hiya all.

I'm trying to capture which key is pressed when a hyperlink is active,
which I'd thought would be simpel.
However - IE returns undefined and FireFox returns 0 when I try to
alert the keycode value, which means the event fires, but I apparently
can't get the keycode which strikes me as odd.

The code I use looks something like this (simplified):
<a href="somelink" onkeypress="return trapEnterNote(1, event)"><img
src="someimage"></a>

Then in my function I do this:

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

This returns "undefined" and 0 as mentioned in the "alert(keycode);".

Is it not possible to get which key was pressed on a hyperlink, is it
only for input fields? Or is it me that does something wrong, and if so
.... any hints/help?

Regards
Xandax

Oct 3 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Oh - FYI,but it is somekind of copy/paste error which made my function
TrapEnter appear twice. It is not twice in my code.

Oct 3 '05 #2

P: n/a
Xandax wrote:
Hiya all.

I'm trying to capture which key is pressed when a hyperlink is active,
which I'd thought would be simpel.
However - IE returns undefined and FireFox returns 0 when I try to
alert the keycode value, which means the event fires, but I apparently
can't get the keycode which strikes me as odd.

The code I use looks something like this (simplified):
<a href="somelink" onkeypress="return trapEnterNote(1, event)"><img
src="someimage"></a>

Then in my function I do this:

function trapEnterNote(intID, e)
{
var keycode;
if (window.event) keycode = window.event.keycode;
You are after keyCode, not keycode.

Try the concise version below:

function trapEnterNote(intID, e)
{
var e = e || window.event;
var keycode = e.which || e.keyCode;
if (keycode == 13) alert('Whiskey Tango Foxtrot');
}
[...]
Is it not possible to get which key was pressed on a hyperlink, is it
only for input fields? Or is it me that does something wrong, and if so
... any hints/help?


'A' elements have onkeypress events too:

<URL:http://www.w3.org/TR/html4/struct/links.html#edef-A>
--
Rob
Oct 3 '05 #3

P: n/a
Thanks - that capital C was exactly my problem :o
Regards

Oct 3 '05 #4

P: n/a


You get nothing because onkeypress is an string-containing r/w element
event handler, like an text field or a textarea or document, as far as I
known, a link element doesn't have a keypress event, what you can do is
engage it for its parent or grandparent which may have it, like:
<a href="..." onclick="document.onkeypress=soso;">
function soso(ev) {
asciiVal=(window.external) ? event.keyCode :
ev.keyCode;

alert(asciiVal);

}

IE uses event on the called function, mozilla uses the 1st arguement.

Danny
Oct 4 '05 #5

P: n/a

Danny skrev:
You get nothing because onkeypress is an string-containing r/w element
event handler, like an text field or a textarea or document, as far as I
known, a link element doesn't have a keypress event, what you can do is
engage it for its parent or grandparent which may have it, like:
<a href="..." onclick="document.onkeypress=soso;">
function soso(ev) {
asciiVal=(window.external) ? event.keyCode :
ev.keyCode;

alert(asciiVal);

}

IE uses event on the called function, mozilla uses the 1st arguement.

Danny


As written in my reply from RobG and my acknowlegdement of the post,
the problem is solved. Hyperlinks have infact keypressed events, my
problem was that I'd used a noncapital c in the keycode where it should
have been keyCode.
Once that was changed, all worked as I wished it to do :)
Xandax

Oct 4 '05 #6

P: n/a
On 02/10/2005 07:31, Danny wrote:

[snip]
function soso(ev) {
asciiVal=(window.external) ? event.keyCode : ev.keyCode;


Exactly what relationship do you think the global external property has
with the event model of any particular host? Do not infer behaviour from
unrelated object model features.

var global = this;

function myFunction(event) {
var keyCode;

if((event = event || global.event)) {
keyCode = ('number' == typeof event.keyCode)
? event.keyCode
: event.which;

if(keyCode) {
/* ... */
}
}
}

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 4 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.