Keyboard-navigable JavaScript widgets

Keyboard-navigable JavaScript widgets

Overview

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 <div> and <span> 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

Originally introduced as part of HTML 4, the tabindex attribute gives authors the means to define the order in which elements will receive focus when navigated by the user via the keyboard. The detailed behavior has now changed and is covered in the HTML 5 draft specs. All major browsers now implement the revised design.

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.
Positive (e.g. tabindex="33") Yes tabindex value determines where this element is positioned in the tab order: In general, elements will be positioned in the tab order before elements that have tabindex="0" or that are naturally tabbable; Specifically, smaller values will position elements earlier in the tab order than larger values (for example, tabindex="7" will be positioned before tabindex="11")

Simple controls

To make a simple widget tab navigable, use tabindex="0" on the <div> or <span> representing it. Here's an example of a span-based checkbox using this technique.

Example 1: A simple image-based checkbox widget using tabindex to allow keyboard access

<!-- Without the tabindex attributes, the <span> elements would not be keyboard focusable -->
<div>
    <span role="checkbox" aria-checked="true" tabindex="0">
        <img src="checked.gif" role="presentation" alt="" />
        Include decorative fruit basket
    </span>
</div>
<div>
    <span role="checkbox" aria-checked="true" tabindex="0">
        <img src="checked.gif" role="presentation" alt="" />
        Include singing telegram
    </span>
</div>
<div>
    <span role="checkbox" aria-checked="false" tabindex="0">
        <img src="unchecked.gif" role="presentation" alt="" />
        Require payment before delivery
    </span>
</div>

Grouping controls

For grouping widgets such as menus, tab panels, grids, or tree views, the parent element should be in the tab order (tabindex="0"), and each descendent choice/tab/cell/row should be removed from the tab order (tabindex="-1"). Users should be able to navigate the descendent elements using arrow keys. (For a full description of the keyboard support that is normally expected for typical widgets, see the DHTML Style Guide.)

The example below shows this technique used with a nested menu control. Once keyboard focus lands on the containing <ul> 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.

Example 2: A menu control using tabindex to control keyboard access

<ul id="mb1" tabindex="0">
  <li id="mb1_menu1" tabindex="-1"> Font
    <ul id="fontMenu" title="Font" tabindex="-1">
      <li id="sans-serif" tabindex="-1">Sans-serif</li>
      <li id="serif" tabindex="-1">Serif</li>
      <li id="monospace" tabindex="-1">Monospace</li>
      <li id="fantasy" tabindex="-1">Fantasy</li>
    </ul>
  </li>
  <li id="mb1_menu2" tabindex="-1"> Style
    <ul id="styleMenu" title="Style" tabindex="-1">
      <li id="italic" tabindex="-1">Italics</li>
      <li id="bold" tabindex="-1">Bold</li>
      <li id="underline" tabindex="-1">Underlined</li>
    </ul>
  </li>
  <li id="mb1_menu3" tabindex="-1"> Justification
    <ul id="justificationMenu" title="Justication" tabindex="-1">
      <li id="left" tabindex="-1">Left</li>
      <li id="center" tabindex="-1">Centered</li>
      <li id="right" tabindex="-1">Right</li>
      <li id="justify" tabindex="-1">Justify</li>
    </ul>
  </li>
</ul>

Disabled controls

When a custom control becomes disabled, remove it from the tab order by setting 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

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:

  1. Roving tabindex: programmatically moving focus
  2. aria-activedescendant: managing a 'virtual' focus

Technique 1: Roving tabindex

Setting the 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:

Bind a key down handler to each element in the group, and when an arrow key is used to move to another element:

  1. programmatically apply focus to the new element,
  2. update the tabindex of the focused element to "0", and
  3. update the tabindex of the previously focused element to "-1".

Here's an example of a WAI-ARIA tree view using this technique.

Tips
Use element.focus() to set focus

Do not use createEvent(), initEvent() and dispatchEvent() to send focus to an element. DOM focus events are considered informational only: generated by the system after something is focused, but not actually used to set focus. Use element.focus() instead.

Use onfocus to track the current focus

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. Track focus using onfocus and onblur instead.

onfocus and onblur can now be used with every element. There is no standard DOM interface to get the current document focus, so if you want to track that you'll have to keep track of it in a JavaScript variable.

Technique 2: aria-activedescendant

This technique involves binding a single event handler to the container widget and using the aria-activedescendent to track a "virtual" focus. (For more information about ARIA, see this overview of accessible web applications and widgets.)

The aria-activedescendant property identifies the ID of the descendent 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). See the source code of this ARIA radiogroup example for a direct illustration of how this works.

Tips
scrollIntoView

Note that the use of this pattern requires the author to ensure that the current focused widget is scrolled into view. You should be able to use the element.scrollIntoView() function, but we recommend confirming this works for you in your target browsers using the quirksmode test.

Issues

General Guidelines

Use onkeydown to trap key events, not onkeypress

IE will not fire keypress events for non-alphanumeric keys. Use onkeydown instead.

Ensure that keyboard and mouse produce the same experience

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 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

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 onclick="doSomething()", you should bind doSomething() to the key down event as well: onkeydown="return event.keyCode != 13 || doSomething();".

Don't use :focus to style the focus (if you care about IE 7 and earlier)

IE 7 and earlier versions do not support the :focus pseudo-selector; do not use it to style focus. Instead, set the style in an onfocus event handler, for example by adding a CSS style name to the class attribute.

Always draw the focus for tabindex="-1" items and elements that receive focus programatically

IE will not automatically draw the focus outline for items that programatically receive focus. Choose between changing the background color via something like this.style.backgroundColor = "gray"; or add a dotted border via this.style.border = "1px dotted invert". In the dotted border case you will need to make sure those elements have an invisible 1px border to start with, so that the element doesn't grow when the border style is applied (borders take up space, and IE doesn't implement CSS outlines).

Prevent used key events from performing browser functions

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 false, the event will not be propagated beyond your handler.

For example:

<span tabindex="-1" onkeydown="return handleKeyDown();">

If 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

Unfortunately onkeydown may or may not repeat depending on what browser and OS you're running on.

Document Tags and Contributors

 Contributors to this page: 6112, diiq, ddal, Tigt, Sheppy, Jifeon, Aaronlev, anjan.ux, anastasia, peterbraden
 Last updated by: 6112,