user1108948
user1108948

Reputation:

toggle buttons using tab

It is simple. I have two buttons in a web page. They are sitting closely. Currently I use mouse to click one of them then doing something. Can we just use keyboard such as TAB?

Thanks.

Upvotes: 1

Views: 214

Answers (3)

John S
John S

Reputation: 573

jQuery would be perfect for this just bind the key to that button. something like this.

$('your selector here').keyup(function(e){
    if(e.which == '9'){
     //do your stuff here
     }
});

I think 9 is the correct charcode for tab but you might need to check that. and make sure you do this inside of $(document).ready();

Upvotes: 1

Widor
Widor

Reputation: 13275

Yes, hit Tab until the button has a dotted border to indicate focus, then you can hit Enter or Space to click the button.

E.g. Button 2 has the focus here and pressing Space or Enter would "click" it:

enter image description here

Upvotes: -1

WhyNotHugo
WhyNotHugo

Reputation: 9926

Using tab to press buttons will completely break accessiblity on you websites and will effectively drive away any keyboard users from your website, it's an awful practice.

In any case, you might want to capture events for '*' using jquery:

$('*').keydown(function(event) {
  if (event.keyCode == 9)
    //stuff
})

Though I'd strongly recomend agaist doing this; never override what keys such as tab do, you'll create huge accessiblity issues.

[EDIT]: Adding the event to document might be more efficient:

If key presses anywhere need to be caught (for example, to implement global shortcut keys on a page), it is useful to attach this behavior to the document object. Because of event bubbling, all key presses will make their way up the DOM to the document object unless explicitly stopped.

Source

Upvotes: 3

Related Questions