YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

YUI Library Home

YUI Library Examples: AutoComplete Control: Basic Local Data

AutoComplete Control: Basic Local Data

This AutoComplete implementation points to a JavaScript array that is available in-memory, allowing for a zippy user interaction without the need for a server-side component. Enabling the prehighlightClassName and useShadow features, as well as pulling in the Animation utility, provides an ehanced visual user experience.

Implementers who are working with data from third-party sources, user input data, or otherwise untrustworthy sources should be sure to read the Security Considerations section of the AutoComplete user guide.

Sample Code

Data:

1YAHOO.example.Data.arrayStates = [ 
2    "Alabama"
3    "Alaska"
4    "Arizona"
5    "Arkansas"
6    "California"
7    "Colorado"
8    "Connecticut"
9    "Delaware"
10    "Florida"
11    ... 
12]; 
view plain | print | ?

CSS:

1#myAutoComplete { 
2    width:15em/* set width here or else widget will expand to fit its container */ 
3    padding-bottom:2em
4} 
view plain | print | ?

Markup:

1<label for="myInput">Enter a state:</label> 
2<div id="myAutoComplete"
3    <input id="myInput" type="text"
4    <div id="myContainer"></div> 
5</div> 
view plain | print | ?

JavaScript:

1YAHOO.example.BasicLocal = function() { 
2    // Use a LocalDataSource 
3    var oDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.arrayStates); 
4    // Optional to define fields for single-dimensional array 
5    oDS.responseSchema = {fields : ["state"]}; 
6 
7    // Instantiate the AutoComplete 
8    var oAC = new YAHOO.widget.AutoComplete("myInput""myContainer", oDS); 
9    oAC.prehighlightClassName = "yui-ac-prehighlight"
10    oAC.useShadow = true
11     
12    return { 
13        oDS: oDS, 
14        oAC: oAC 
15    }; 
16}(); 
view plain | print | ?

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:

Logger Console

INFO 146ms (+0) 8:43:15 AM:

LogReader instance0

LogReader initialized

INFO 146ms (+1) 8:43:15 AM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 145ms (+0) 8:43:15 AM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 145ms (+50) 8:43:15 AM:

Get

_next: q0, loaded: undefined

INFO 95ms (+2) 8:43:15 AM:

AutoComplete instance0 myInput

AutoComplete initialized

INFO 93ms (+92) 8:43:15 AM:

DataSource instance0

DataSource initialized

INFO 1ms (+0) 8:43:15 AM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More AutoComplete Control Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

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