March 9, 2021
Hot Topics:

Fun with Auto-Complete Widgets

  • By Vlad Kofman
  • Send Email »
  • More Articles »

Unlike Yahoo, Google only shows the number of results and does not show similar or related keywords—called "concepts." Yahoo also shows text queries where the keyword is in the middle or the end. For example, a search for "java" also suggests "free java download."

Click here for a larger image.

Figure 4: Yahoo search assist

To implement this widget on your site, you can write all the logic from scratch or use one of the available AJAX widgets. I will look at widgets from Dojo, Scriptaculous, and Yahoo User Interface (YUI) libraries. They are all conceptually similar. They all send a asynchronous request to the server as soon as the user starts to type. As the response is received, they open a dialog on the client side, and populate it with suggestions from the server. Because this technology is server-side agnostic, you can use any application server on the back end.

Implementation in Dojo

To add a Dojo auto-complete combo box, you need to import main Dojo libraries (either on the page or in the main header if it is reused throughout the site).

You also need to declare dojo.widget.ComboBox and put the <select> tag with dojo parameters where you want the auto-complete text-field to appear.

Note: The select actually will be dynamically modified by the Dojo toolkit.

Also, note that one of the parameters, called dataUrl, needs to point to the server URL that will return suggestions.

<script type="text/javascript" src="dojo.js"></script>
<script type="text/javascript">

<select dojoType="ComboBox" value=""
   style="width: 200px;"
   name="auto_complete" maxListLength="15">

That is really all that you need to do to incorporate Dojo based auto-complete on the client side. Again, the server implementation could use any technology.

Figure 5 shows the result of typing just the letter "a" in the US state's auto suggest field.

Figure 5: Dojo Auto-complete demo

Please see the included source code for the full demo.

Implementation in the Scriptaculous Toolkit

Scriptaculous libraries are very similar in setup as those in Dojo, but require a little more code, and a specific response text format.

You need to define specific style for the div element.

div.autocomplete {
   border:1px solid #888;
div.autocomplete ul {
div.autocomplete ul li.selected { background-color: #ffb;}
div.autocomplete ul li {
   font-family:Verdana, Arial, Helvetica, sans-serif;

You also need to import Scriptaculous JavaScript files on either the page where this feature should become visible or in the main header, if it is reused throughout the site.

<script language="JavaScript"
<script language="JavaScript"

Page 2 of 4

This article was originally published on January 3, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date