I am taking a look into the JQuery AutoComplete widget and wanted to create an example with the most simple and scaled down functionality possible. In the image to the left is an example of what this looks like in action. It is basically an HTML text area that is hooked up to a static array of options.
What is an AutoComplete Widget?
An AutoComplete widget basically does what it sounds like. It can be added to an HTML control such as a text input area so that once a user starts typing in the area a narrowing list of options appears. The simplest way to populate these options is using a hard-coded array (as I will illustrate in this example), but naturally such hard-coding is also the least flexible and maintainable.
Two other options for populating the AutoComplete widget are Ajax calls or a Callback. These two options are great since the data can dynamically be retrieved from a database or similar data source, and they also scale very well once your options arrays reach several hundred options in size.
Here is the code:
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script> <script> $(function() { var itemList = [ "Alfa","Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel", "India","Juliett","Juliet","Kilo","Lima","Mike","November","Oscar","Papa" ]; $("#form1 input#autoCompleteTest").autocomplete({ source: itemList }); }); </script> </head> <body> <form id="form1" name="form1"> Autocomplete test area <input type="text" id="autoCompleteTest" class="ui"> </form></body> </html>
Resources:
- AF-Design – Using jQuery UI’s Autocomplete to Populate a Form
- Net.tutsplus.com – How to Use the jQuery UI Autocomplete Widget
- JQuery User Interface – AutoComplete Documentation
- Return-true.com – AutoComplete Example
- Ajaxdaddy.com – Ajax AutoComplete Example
- Stackoverflow.com – jquery ui accordions within tabs
Leave a comment