Keyboard-navigable JavaScript widgets

Web applications often use JavaScript to mimic desktop widgets such as menus, tree views, rich text fields, and tab panels. These widgets are typically composed of

and elements that do not, by nature, offer the same keyboard functionality that their desktop counterparts do. This document describes techniques to make JavaScript widgets accessible with the keyboard.

Using tabindex

By default, when people use the tab key to browse a webpage, only interactive elements (like links, form controls) get focused. With the tabindex global attribute, authors can make other elements focusable, too. When set to 0, the element becomes focusable by keyboard and script. When set to -1, the element becomes focusable by script, but it does not become part of the keyboard focus order.

The order in which elements gain focus when using a keyboard, is the source order by default. In exceptional circumstances, authors may want to redefine the order. To do this, authors can set tabindex to any positive number.

Warning: Avoid using positive values for tabindex. Elements with a positive tabindex are put before the default interactive elements on the page, which means page authors will have to set (and maintain) tabindex values for all focusable elements on the page whenever they use one or more positive values for tabindex.

The following table describes tabindex behavior in modern browsers:

tabindex attribute Focusable with mouse or JavaScript via element.focus() Tab navigable
not present Follows the platform convention of the element (yes for form controls, links, etc.). Follows the platform convention of the element.
Negative (i.e., tabindex="-1") Yes No; author must focus the element with focus() in response to arrow or other key presses.
Zero (i.e., tabindex="0") Yes In tab order relative to element's position in document (note that interactive elements like have this behavior by default, they don't need the attribute).
Positive (e.g., tabindex="33") Yes tabindex value determines where this element is positioned in the tab order: smaller values will position elements earlier in the tab order than larger values (for example, tabindex="7" will be positioned before tabindex="11").

Non-native controls

Native HTML elements that are interactive, like , and