WebAIM - Web Accessibility In Mind

E-mail List Archives

Re: onClick and onKeypress

for

From: Alex Bovey
Date: Oct 4, 2005 9:20AM


Adding an onkeypress event handler won't help at all if they don't
have Javascript enabled, since the onkeypress will still call a
Javascript function. However, onclick assumes that your users are
using a mouse, which they may not be, and that's the reason for using
onkeypress as well. So what you would do is this:

<a href="#" onclick="myJavascriptFunction()" onkeypress="this.onclick()">

This means that if they're using a keyboard they will still be able to
call the function, and whatever happens the event won't be called
twice.

In order to cater for the people that don't have javascript enabled
you should put a link to a page in the href attribute that will act as
an alternative:

<a href="my_alternative.html" onclick="myJavascriptFunction(); return
false;" onkeypress="this.onclick()">

Notice the "return false" that's been added after the call to the
Javascript function. This is to prevent the href being followed AND
the Javascript function being called, which you probably don't want.

HTH,

Alex

--
Alex Bovey