YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: AutoComplete Control: Searching Field A, Submitting Field B with itemSelectEvent

# AutoComplete Control: Searching Field A, Submitting Field B with itemSelectEvent

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. The data consists of an account name, and an account number. The AutoComplete allows the user to search by name, but by subscribing to the itemSelectEvent Custom Event, populates a hidden form field with the ID, which the server would need for processing the hypothetical submission.

View example in new window.

### Find a company in the accounts database:

## Sample Code

Data:

YAHOO.example.Data.accounts: [ {name: "ABC Company", id: 57367 }, {name: "Acme Supply Company", id: 84377}, {name: "Avery Widgets", id: 73678}, {name: "AAA International", id: 73675}, {name: "Atlantic Brothers, Inc", id: 83757}, {name: "Ace Products", id: 48588}, {name: "Above Average, Ltd", id: 75968} ];

CSS:

#myAutoComplete { width:15em; /* set width here or else widget will expand to fit its container */ padding-bottom:2em; } #mySubmit { position:absolute; left:15em; margin-left:1em; /* place the button next to the input */ }

Markup:

<form id="myForm" action="#"> <div id="myAutoComplete"> <input id="myInput" type="text"><input id="mySubmit" type="submit" value="Submit"> <div id="myContainer"></div> </div> <input id="myHidden" type="hidden"> </form>

JavaScript:

YAHOO.example.ItemSelectHandler = function() { // Use a LocalDataSource var oDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.accounts); oDS.responseSchema = {fields : ["name", "id"]}; // Instantiate the AutoComplete var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS); oAC.resultTypeList = false; // Define an event handler to populate a hidden form field // when an item gets selected var myHiddenField = YAHOO.util.Dom.get("myHidden"); var myHandler = function(sType, aArgs) { var myAC = aArgs[0]; // reference back to the AC instance var elLI = aArgs[1]; // reference to the selected LI element var oData = aArgs[2]; // object literal of selected item's result data // update hidden form field with the selected item's ID myHiddenField.value = oData.id; }; oAC.itemSelectEvent.subscribe(myHandler); // Rather than submit the form, // alert the stored ID instead var onFormSubmit = function(e, myForm) { YAHOO.util.Event.preventDefault(e); alert("Company ID: " + myHiddenField.value); }; YAHOO.util.Event.addListener(YAHOO.util.Dom.get("myForm"), "submit", onFormSubmit); 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: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### AutoComplete Control Examples:

- Basic Local Data
- Basic Remote Data
- 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 © 2009 Yahoo! Inc. All rights reserved.

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