YUI recommends YUI3.

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

This documentation is no longer maintained.

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.

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.accounts: [ 
2    {name: "ABC Company", id: 57367 }, 
3    {name: "Acme Supply Company", id: 84377}, 
4    {name: "Avery Widgets", id: 73678}, 
5    {name: "AAA International", id: 73675}, 
6    {name: "Atlantic Brothers, Inc", id: 83757}, 
7    {name: "Ace Products", id: 48588}, 
8    {name: "Above Average, Ltd", id: 75968} 
9]; 
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} 
5#mySubmit { 
6    position:absoluteleft:15emmargin-left:1em/* place the button next to the input */ 
7} 
view plain | print | ?

Markup:

1<form id="myForm" action="#"
2<label for="myInput">Find a company in the accounts database:</label> 
3    <div id="myAutoComplete"
4        <input id="myInput" type="text"><input id="mySubmit" type="submit" value="Submit">  
5        <div id="myContainer"></div> 
6    </div> 
7    <input id="myHidden" type="hidden"
8</form> 
view plain | print | ?

JavaScript:

1YAHOO.example.ItemSelectHandler = function() { 
2    // Use a LocalDataSource 
3    var oDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.accounts); 
4    oDS.responseSchema = {fields : ["name""id"]}; 
5 
6    // Instantiate the AutoComplete 
7    var oAC = new YAHOO.widget.AutoComplete("myInput""myContainer", oDS); 
8    oAC.resultTypeList = false
9     
10    // Define an event handler to populate a hidden form field 
11    // when an item gets selected 
12    var myHiddenField = YAHOO.util.Dom.get("myHidden"); 
13    var myHandler = function(sType, aArgs) { 
14        var myAC = aArgs[0]; // reference back to the AC instance 
15        var elLI = aArgs[1]; // reference to the selected LI element 
16        var oData = aArgs[2]; // object literal of selected item's result data 
17         
18        // update hidden form field with the selected item's ID 
19        myHiddenField.value = oData.id; 
20    }; 
21    oAC.itemSelectEvent.subscribe(myHandler); 
22     
23    // Rather than submit the form, 
24    // alert the stored ID instead 
25    var onFormSubmit = function(e, myForm) { 
26        YAHOO.util.Event.preventDefault(e); 
27        alert("Company ID: " + myHiddenField.value); 
28    }; 
29    YAHOO.util.Event.addListener(YAHOO.util.Dom.get("myForm"), "submit", onFormSubmit); 
30 
31    return { 
32        oDS: oDS, 
33        oAC: oAC 
34    }; 
35}(); 
view plain | print | ?

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings