YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: AutoComplete Control: Screenreader Accessibility

# AutoComplete Control: Screenreader Accessibility

This AutoComplete example demonstrates how to make the widget more screenreader accessible.

View example in new window.

Enter a state:

There are a few simple things you should do to make your AutoComplete implementation more screenreader accessible.

## Label your input field

First, provide a label element for your input field. Within this label, you can include a span that is styled via CSS to be displayed offscreen with helpful text that screenreaders will read aloud.

## Insert the screenreader label text dynamically

If you insert the span element that holds the screenreader text into the label dynamically at runtime, most screenreaders will read it aloud each time only when the user focuses on the input for a new AutoComplete interaction, instead of during regular page browsing.

## Enable the `typeAhead` feature

By setting your AutoComplete's `typeAhead` property to true, screenreaders (in focus mode) will read aloud each suggestion as the user navigates them with the up and down arrow keys.

## Disable the `autoHighlight` feature

By default, AutoComplete's `autoHighlight` property is enabled, which may add confusion when screenreaders read aloud the first suggestion when it is autohighlighted. To avoid this scenario, disable the `autoHighlight` feature.

## Sample Code

Markup:

<div id="myAutoComplete"> <label id="myInputLabel" for="myInput">Enter a state:</label> <input id="myInput" type="text"> <div id="myContainer"></div> </div>

JavaScript:

YAHOO.example.Accessibility = function() { // Set up DataSource and AutoComplete instances var oDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.arrayStates, {responseSchema: {fields : ["state"]}}); var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS); // Set up span element with screenreader text var elLabel = YAHOO.util.Dom.get("myInputLabel"), origLabel = elLabel.innerHTML, screenreaderLabel = '<span style="position:absolute;left:-999em;">Use the arrow keys to navigate suggestions.</span>'; // Insert the screenreader text whenever user starts an AutoComplete interaction oAC.textboxFocusEvent.subscribe(function(){elLabel.innerHTML += screenreaderLabel;}); oAC.textboxBlurEvent.subscribe(function(){elLabel.innerHTML = origLabel;}); // The typeAhead feature must also be set to true for screenreader support oAC.typeAhead = true; // Turn off autoHighlight for less confusion oAC.autoHighlight = false; return { oDS: oDS, oAC: oAC }; }();

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### AutoComplete Control Examples:

### More AutoComplete Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings