jQuery - Trigger click event on links with spacebar?
Posted
by Herb Caudill
on Stack Overflow
See other posts from Stack Overflow
or by Herb Caudill
Published on 2010-03-12T16:04:18Z
Indexed on
2010/03/12
16:07 UTC
Read the original article
Hit count: 372
It looks like in most browsers, an <input type="submit">
treats both [spacebar] and [enter] as a click, but an <a>
link only treats [enter] as a click.
My app uses a number of links formatted to simulate buttons, so a user that is accustomed to tabbing to a button and pressing [spacebar] will be frustrated.
This bit of jQuery solves the problem:
$("a.Button").die("keypress").live("keypress", function(e) {
if (e.which == 32) {
$(this).trigger("click");
e.preventDefault();
}
});
My question: Is this a reason not to do this? I'm a little reluctant to override the browser's default behavior on something as basic as this, but since I'm already abusing the link tag to make it look like a button, at least this way I'm not violating the user's expectations any further.
© Stack Overflow or respective owner