onClick vs onKeyPress

Donald F. Evans

November 2005

Question

I have a question for you about accessibility and the JavaScript onClick and onKeyPress handlers.

Many of our links (anchor tags) contain onClick handlers. In my testing, it seems like using the keyboard to "execute" a link (clicking ENTER when the focus is on the link in my browser) does fire an onClick event, even though the mouse wasn't used to "execute" the link.

Is this your belief as well? If so, is there any reason why we'd also need onKeyPress handlers to cover areas where we're using onClick events?

Thanks,
Mike Boyers


Answer

Originally onClick did not work from the keyboard in all browsers and all screen readers. Recently however, all known browsers have mapped on-click to the return key. This works well in Window Eyes and Home Page Reader as well as Jaws.

A word of caution. onKeyPress has some bugs in IE6 originally noted in IE5, but never fixed. When using JavaScript to program access keys for navigation, the only event that works consistently across all browsers is onKeyUp.

So, basically, this requirement is a hold over from days when all browsers did not support onClick from the keyboard, and there is no longer a need to make a call to onKeyPress every time you make a call to onClick.


Other Resources