PDA

View Full Version : JavaScipt onkeypress doesn't work the way I thought


thendis
06-06-2005, 06:42 AM
Hellooo everyone!

I have a site which uses a user-controlled style sheet script. My working directory is here (links I am referring to are up top-right):

http://users.on.net/~longo/forum/working/default.htm

Now, to comply with W3C's guideline about keeping JavaScripts device-independant, I have used their suggestion by using "onClick" along with "onKeyDown" on those links for calling on the "changeToAccessible" function.

The problem is that on some browsers onkeypress seems to work the same as onblur, because if you tab off one of those links, the function is activated!!! what's up with that??

I would like to have it so that the function is only called upon when the link is activated by tabbing to it, then pressing the Enter key.

Can that be done easily?

Keep in mind I know very little about JavaScript. The script that is used on the site was created from a cut-and-paste tutorial!

:)

Thanx in advance!

Goldilocks
06-06-2005, 07:33 AM
The link you posted doesn't work. When you tab off of a link, you are pressing a key and that is why it is activating the function. You may be able to restrict this trigger to the Enter key by using the javascript event.keycode (which I believe should equal 13 for the Enter key).

It's not something I have used a great deal before so I can't give you the exact code but I'll move this thread over to the Client-Side Scripting forum where you may have more luck.

thendis
06-06-2005, 10:05 AM
hey Goldilocks, thanks for that.

so, how would event.keycode be used to activate the ChangeToAccessible function? I know nothing about JS (i'm sure that's blatantly obvious).

Goldilocks
06-06-2005, 11:49 AM
Well like I said, I haven't used event.keycode myself much and I can't see your code to show you exactly how to edit it but it would be something along the lines of:


<a href="whatever" onkeypress="if (event.keycode==13) changeToAccessible();">Link Text</a>

thendis
06-06-2005, 12:07 PM
<a href="whatever" onkeypress="if (event.keycode==13) changeToAccessible();">Link Text</a>


worked like a friggin' charm mate! Thanks so much for that!!

Goldilocks
06-07-2005, 02:58 AM
Great! Glad to help! ;)