Techniques for WCAG 2.0

On this page:

-

# SCR29: Adding keyboard-accessible actions to static HTML elements

## Applicability

HTML and XHTML, Script

This technique relates to:

- Success Criterion 2.1.1 (Keyboard)
  - How to Meet 2.1.1 (Keyboard)
  - Understanding Success Criterion 2.1.1 (Keyboard)
- Success Criterion 2.1.3 (Keyboard (No Exception))
  - How to Meet 2.1.3 (Keyboard (No Exception))
  - Understanding Success Criterion 2.1.3 (Keyboard (No Exception))

## User Agent and Assistive Technology Support Notes

HTML 4.01 only defines the tabindex attribute for `a`, `area`, `button`, `input`, `object`, `select`, and `textarea`, and limits its value to the range between 0 and 32767. The use of `tabindex` with other element types and the tabindex value -1 is supported in Internet Explorer 5.01 and higher, and Firefox 1.5 and higher, Opera 9.5 and higher and Camino. Note that modifying focus through script can cause unpredictable behavior in screen readers that use a virtual cursor.

## Description

The objective of this technique is to demonstrate how to provide keyboard access to a user interface control that is implemented by actions to static HTML elements such as `div` or `span`. This technique ensures that the element is focusable by setting the `tabindex` attribute, and it ensures that the action can be triggered from the keyboard by providing an `onkeyup` or `onkeypress` handler in addition to an `onclick` handler.

When the `tabindex` attribute has the value 0, the element can be focused via the keyboard and is included in the tab order of the document. When the `tabindex` attribute has the value -1, the element cannot be tabbed to, but focus can be set programmatically, using `element.focus()`.

Because static HTML elements do not have actions associated with them, it is not possible to provide a backup implementation or explanation in environments in which scripting is not available. This technique should only be used in environments in which client-side scripting can be relied upon.

Note: Such user interface controls must still satisfy Success Criterion 4.1.2. Applying this technique without also providing role, name, and state information about the user interface control will results in Failure F59, Failure of Success Criterion 4.1.2 due to using script to make div or span a user interface control in HTML.

## Examples

### Example 1: Adding a JavaScript action to a div element

The `div` element on the page is given a unique `id` attribute and a `tabindex` attribute with value 0. A script uses the Document Object Model (DOM) to find the `div` element by its id and add the `onclick` handler and the `onkeyup` handler. The `onkeyup` handler will invoke the action when the Enter key is pressed. Note that the `div` element must be loaded into the DOM before it can be found and modified. This is usually accomplished by calling the script from the `onload` event of the `body` element. The script to add the event handlers will only execute if the user agent supports and has JavaScript enabled.

Example Code:

    ...
    <script type="text/javascript">
     // this is the function to perform the action. This simple example toggles a message.
     function doSomething(event) {
       var msg=document.getElementById("message");
       msg.style.display = msg.style.display=="none" ? "" : "none";
       //return false from the function to make certain that the href of the link does not get invoked
       return false;
     }
     // this is the function to perform the action when the Enter key has been pressed.
     function doSomethingOnEnter(event) {
       var key = 0;
       // Determine the key pressed, depending on whether window.event or the event object is in use
       if (window.event) {
         key = window.event.keyCode;
       } else if (event) {
         key = event.keyCode;
       }
       // Was the Enter key pressed?
       if (key == 13) {
         return doSomething(event);
       }
       // The event has not been handled, so return true
       return true;
     }
     // This setUpActions() function must be called to set the onclick and onkeyup event handlers onto the existing
     // div element. This function must be called after the div element with id="active" has been loaded into the DOM.
     // In this example the setUpActions() function is called from the onload event for the body element.
     function setUpActions() {
       // get the div object
       var active=document.getElementById("active");
       // assign the onclick handler to the object.
       // It is important to return false from the onclick handler to prevent the href attribute
       // from being followed after the function returns.
       active.onclick=doSomething;
       // assign the onkeyup handler to the object.
       active.onkeyup=doSomethingOnEnter;
     }
     </script>

     <body onload="setUpActions();">
     <p>Here is the link to modify with a javascript action:</p>
     <div>
      <span id="active" tabindex="0">Do Something</span>
     </div>
     <div id="message">Hello, world!</div>
    ...

Here is a working example of this code: Creating Divs with Actions using JavaScript.

## Resources

Resources are for information purposes only, no endorsement implied.

- HTML 4.01 Scripts

- HTML 4.01 Giving focus to an element

- Accessible Rich Internet Applications (WAI-ARIA) Version 1.0 Global States and Properties

- WAI-ARIA Primer, Provision of the keyboard or input focus

- Document Object Model (DOM) Technical Reports

- Firefox support for ARIA: Accessible Rich Internet Applications

- Internet Explorer, HTML and DHTML Reference, tabIndex Property

## Related Techniques

- SCR20: Using both keyboard and other device-specific functions
- SCR24: Using progressive enhancement to open new windows on user request
- SCR35: Making actions keyboard accessible by using the onclick event of anchors and buttons
- F59: Failure of Success Criterion 4.1.2 due to using script to make div or span a user interface control in HTML

## Tests

### Procedure

In a user agent that supports Scripting:

1. Click on the control with the mouse

2. Check that the scripting action executes properly

3. Check that it is possible to navigate to and give focus to the control via the keyboard

4. Set keyboard focus to the control

5. Check that pressing ENTER invokes the scripting action.

### Expected Results

- All of the checks are true

- Top
- Contents
- Intro
- Previous: Technique SCR28
- Next: Technique SCR30