Adverts

Archives By Subject

Calendar

Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Search

RSS


Tags

adobe book review cfimage coldfusion google chrome hosting iis internet explorer java javascript jquery lucene photoshop regex ses urls software review sql injection svn trac

Subscribe

Enter your email address to subscribe to this blog.

Turn SELECTs into Comboboxes with jQuery UI Autocomplete

This entry is about a script I've written using jQuery, that turns ordinary html select elements into comboboxes. You don't need to re-write any of your code, and users without JavaScript will still have fully functioning select boxes.

I've been working on a project on and off for over a year now, and it is huge! I've lost track of the number of pages and forms that have been developed for it.

The end is finally in sight, but a recent request has been to use Comboboxes for selecting users. Currently, the forms use plain old html SELECT tags, and personally I think they're great. I find them very fast to use, and most browsers will filter them if you start to type anyway. However, the client was very insistent on this feature, so I've had to put them in.

As html doesn't have comboboxes, a JavaScript solution was the only option. I had two big requirements though:

  1. The JavaScript must progressively enhance the html - it must still work for users without JS.
  2. I didn't want to spend hours re-working the many forms where this was needed - I didn't want to have to change the back-end processing, I didn't want to set up remote ajax calls for each form and I wanted to minimise front-end html changes.

I'm still fairly new at jquery, but it's been great so far. I hadn't used Jquery UI before, but the autocomplete feature seemed ideal. In particular, this example seemed to be very close to what I wanted.

It soon became obvious that the example hadn't been used in the real world before. If the select was inside a form (a fairly normal occurence), then the form would be submitted as soon as the combobox button was clicked. There were also problems with the select not being updated until focus was lost. Several hours of bug fixing and modifications later, I came up with combobox.js

To use this, you first need to go to the jQuery UI site, and download the appropriate custom package. Make sure you use version 1.8 or higher, and it must include at least the autocomplete and button modules. Just follow the jQuery instructions for installing it on your page.

To install the Combobox functionality, you just need to link to the file after the jQuery file links in the head of your page, as shown in the example below:

<link rel="Stylesheet" type="text/css" href="/css/ui-lightness/jquery-ui-1.8.2.custom.css" />
   <script type="text/javascript" src="/scripts/jquery-1.4.2.min.js"></script>
   <script type="text/javascript" src="/scripts/jquery-ui-1.8.2.custom.min.js"></script>
   <script type="text/javascript" src="/scripts/combobox.js"></script>

Now comes the clever bit - any select tag can be turned into a combobox just by adding the class combobox:

<select name="greeknumber" class="combobox">
      <option value="1">Alpha</option>
      <option value="2">Beta</option>
      <option value="3">Gamma</option>
      <option value="4">Delta</option>
   </select>

That's it! Once you have jQuery and the script installed, you only need to add the class!

Feel free to use the script as you want. I've tested it on a few of the latest browsers, but not all, so please let me know if you find any bugs.

Comments
Emery's Gravatar Hi,
Thank you for this package! Is there anyway for the dropdown widget to refresh? In my code, the dropdown values are dependent on another value. If the value changes, the dropdown values should change (using ajax) as well but it does not seem to refresh in the dropdown. Any help is appreciated. Thanks!

Regards,
Emery
# Posted By Emery | 26/08/10 16:13
Gareth's Gravatar Hi Emery,

This script is really only designed to progressively enhance static SELECTs.
If the SELECT's options are dynamic, then you're probably better with one of the other examples here:
http://jqueryui.com/demos/autocomplete/

Hope this helps,

Gareth
# Posted By Gareth | 26/08/10 16:23
Adobe Certified Professional - Macromedia ColdFusion MX 7 Advanced Developer Powered By ColdFusion 8 aggregated by mxna aggregated by coldfusionBloggers