Creating an autocomplete form field historically has not been a trivial matter and would require an indepth knowledge of JavaScript and CSS. However, the task is made far more simple when using one of the many freely-available JavaScript libraries. In this post I will show you how to implement the jQuery Autocomplete created by Dylan Verheul.
The Goal
Allow the user to type a few characters into a standard form text input field and to automatically provide suggestions from which the user can select.
Prerequisites
- The latest copy of jQuery
- A basic understanding of JavaScript
- A server-side script that can respond to the AJAX request, in our case ColdFusion
Demo
The demo will specifically look at a simple form text input field, which takes a country name.
See the demo, and others, in action
How It Works
Once the user begins to type into the form text input field, the jQuery autocomplete is activated. After a set time interval, a list of items is displayed below the input field. The user can select these with either the arrow keys or mouse.
The Code
There are three parts to this demo:
- The page’s HTML.
- The server-side code to produce the dynamic page (i.e. to load the autocomplete
divwhen the user types something into the input field). - The jQuery & JavaScript.
HTML Form
<h3>Example 1.: Country Lookup</h3> <p>Using <abbr title="Asynchronous JavaScript and XML">AJAX</abbr> to interrogate the database.</p> <p>Example data: Australia, Bulgaria, United Kingdom</p> <form name="frmAutoCompleteCountry" id="frmAutoCompleteCountry" action="#" method="post"> <p> <label for="country">Country</label> <input type="text" name="country" id="country" /> </p> </form> <p>NB. If you have <a href="http://getfirebug.com/" title="Get Firebug">Firebug</a> installed you will be able to view the <abbr title="Asynchronous JavaScript and XML">AJAX</abbr> call.</p>
ColdFusion
This is a simple example, using a database to return a list of country names that match the characters the user has input. You could expand this and return a JSON data structure.
<cfsetting enablecfoutputonly="true"> <cfquery name="qryGetCountry" datasource="myDatasource"> SELECT countryName FROM Country WHERE countryName LIKE <cfqueryparam value="#URL.q#%" cfsqltype="cf_sql_varchar" /> </cfquery> <cfoutput query="qryGetCountry"> #qryGetCountry.countryName##chr(10)# </cfoutput>
JavaScript
The JavaScript will attach itself after the document is ready, i.e. after the page has loaded. Each time the text input field, with the ID of country, is changed, the autocomplete event is fired. This makes a call to the ColdFusion page, which returns a list of matched items.
<script type="text/javascript" src="jquery-1.2.6.min.js"></script> <script type="text/javascript" src="jquery.autocomplete.js"></script> <link type="text/css" href="autocomplete.css" rel="stylesheet" media="screen" /> <script type="text/javascript"> $(document).ready(function() { $("#country").autocomplete( "country.cfm", { minChars:2, delay:200, autoFill:false, matchSubset:false, matchContains:1, cacheLength:10, selectOnly:1 } ); ); </script>
Where To Take It Next
JSON
The above example only shows a simple text list, separated by carriage returns. It is more preferable to use JSON.
Unobtrusive JavaScript
As with any page that is loaded with JavaScript and AJAX functionality, it should work without JavaScript.
To achieve this with the above tutorial, you will need to replace the jQuery autocomplete functionality with an ‘interim’ page that allows a user to select from a list of items, effectively turning the input field into a simple search interface. Of course, all other form field information would need to be retained between pages.
Download The Code
The example code can be downloaded from the demo page. Included are ColdFusion and PHP examples.

Recent Comments