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 By default, when people use the tab key to browse a webpage, only interactive elements (like links, form controls) get focused. With the 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 Warning:
Avoid using positive values for The following table describes Native HTML elements that are interactive, like Authors can also make a For grouping widgets such as menus, tablists, grids, or tree views, the parent element should be in the tab order ( The example below shows this technique used with a nested menu control. Once keyboard focus lands on the containing When a custom control becomes disabled, remove it from the tab order by setting When a user tabs away from a widget and returns, focus should return to the specific element that had focus, for example, the tree item or grid cell. There are two techniques for accomplishing this: Setting the Bind a key down handler to each element in the group, and when an arrow key is used to move to another element: This technique involves binding a single event handler to the container widget and using the The To ensure that the user experience is consistent regardless of input device, keyboard and mouse event handlers should share code where appropriate. For example, the code that updates the To ensure that the keyboard can be used to activate elements, any handlers bound to mouse events should also be bound to keyboard events. For example, to ensure that the Enter key will activate an element, if you have an Ensure that focused elements have a focus ring. This can be done using the CSS If your widget handles a key event, prevent the browser from also handling it (for example, scrolling in response to the arrow keys) by using your event handler's return code. If your event handler returns For example: If Unfortunately 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
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.tabindex
to any positive number.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
.tabindex
behavior in modern browsers:
tabindex
attributeFocusable 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
,
and
, are already accessible by keyboards, so to use one of them is the fastest path to make components work with keyboards.
keyboard accessible by adding a
tabindex
of 0
. This is particularly useful for components that use interactive elements that do not exist in HTML.Grouping controls
tabindex="0"
), and each descendant choice/tab/cell/row should be removed from the tab order (tabindex="-1"
). Users should be able to navigate the descendant elements using arrow keys. (For a full description of the keyboard support that is normally expected for typical widgets, see the WAI-ARIA Authoring Practices.)
element, the JavaScript developer must programmatically manage focus and respond to arrow keys. For techniques for managing focus within widgets, see "Managing focus inside groups" below.
Disabled controls
tabindex="-1"
. Note that disabled items within a grouped widget (such as menu items in a menu) should remain navigable using arrow keys.Managing focus inside groups
tabindex
: programmatically moving focusaria-activedescendant
: managing a 'virtual' focusTechnique 1: Roving tabindex
tabindex
of the focused element to "0" ensures that if the user tabs away from the widget and then returns, the selected item within the group retains focus. Note that updating the tabindex
to "0" requires also updating the previously selected item to tabindex="-1"
. This technique involves programmatically moving focus in response to key events and updating the tabindex
to reflect the currently focused item. To do this:
tabindex
of the focused element to "0", andtabindex
of the previously focused element to "-1".Technique 2:
aria-activedescendant
aria-activedescendant
to track a "virtual" focus. (For more information about ARIA, see this overview of accessible web applications and widgets.)aria-activedescendant
property identifies the ID of the descendant element that currently has the virtual focus. The event handler on the container must respond to key and mouse events by updating the value of aria-activedescendant
and ensuring that the current item is styled appropriately (for example, with a border or background color).General guidelines
Usage of focus events
focus
event to send focus to an element. DOM focus events are considered informational only: they are generated by the system after something is focused, but not actually used to set focus. Use element.focus()
instead.focus
and blur
events to track focus changes. Don't assume that all focus changes will come via key and mouse events: assistive technologies such as screen readers can set the focus to any focusable element. If you want to track the focus status for the whole document, you can use the document.activeElement
to get the active element, or document.hasFocus
to make sure if the current document has focus.Ensure that keyboard and mouse produce the same experience
tabindex
or the styling when users navigate using the arrow keys should also be used by mouse click handlers to produce the same changes.Ensure that the keyboard can be used to activate element
onclick="doSomething()"
, you should bind doSomething()
to the key down event as well: onkeydown="event.code === "Enter" && doSomething();"
.Always draw the focus for tabindex="-1" items and elements that receive focus programmatically
outline
property, which should not be unconditionally set to none
—if you want to prevent unnecessary focus rings being displayed, use the :focus-visible
pseudo-class.Prevent used key events from performing browser functions
false
, the event will not be propagated beyond your handler.…
span.onkeydown = handleKeyDown;
handleKeyDown()
returns false
, the event will be consumed, preventing the browser from performing any action based on the keystroke.Don't rely on consistent behavior for key repeat, at this point
onkeydown
may or may not repeat depending on what browser and OS you're running on.