Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using the piece of code below, I can set focus on a control if pressing the 'RETURN' key, but not on pressing the 'TAB'. How can setting control focus be done also for pressing 'TAB'? Thanks.

JavaScript
if (event.keyCode == 13 || event.keyCode == 9 || k == 0) {  // RETURN or TAB
   ...
   document.getElementById(e.id).focus();
Posted
Comments
ZurdoDev 30-Nov-15 9:52am    
You probably need to add e.preventDefault() to stop the normal tabbing behavior of the page.

1 solution

As RyanDev suggested, the bit you're missing is e.preventDefault().

But why would you change the browser behaviour on tab press?

People with accessibility issues are going to find your website very difficult to use. Lots of people with mobility issues or sight issues are dependant on readers and the limited navigation choices of tab forwards and tab backwards.

Instead you should be managing the order of controls using the tabIndex attribute.

tabIndex Attribute[^]
 
Share this answer
 
Comments
s yu 30-Nov-15 12:29pm    
Thanks for the solution you and RayanDev provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900