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:

- Basic Local Data
- Basic Remote Data
- Screenreader Accessibility
- Customizing Remote Requests
- Custom Formatting, with a Proxyless Remote DataSource
- Searching Field A, Submitting Field B with itemSelectEvent
- Combobox, with CSS for Multiple Stacked Instances
- Tagging Example with alwaysShowContainer
- FunctionDataSource to Search Multiple Fields of an Address Book at Runtime
- Centering AutoComplete On a Page
- Subset Matching
- Find Photos on Flickr
- Configurations Dashboard
- Skinning Model
- Flickr Image Search (included with examples for Rich Text Editor)
- Logger Integration with YUI Components (included with examples for Logger Control)
- Datatable with Autocomplete (included with examples for DataTable Control)
- Complex Application (included with examples for Layout Manager)

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