spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / ncz / column3 / 1 current pageTo page 2To page 3
[next]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Creating an Autosuggest Textbox with JavaScript, Part 3

By Nicholas C. Zakas.

In the second part of this series, you learned how to add a dropdown suggestion list to the autosuggest control that was built in the first part of this series. This article completes the modifications by showing you how to make your suggestions case insensitive and how to get the suggestions back from the server instead of using client-side information.

Case Insensitive Suggestions

I got a lot of feedback from the past two articles asking why the suggestions were case sensitive. The simple answer is that the point of the previous articles was to introduce the methodology for creating the autosuggest control. Now that you understand the basics, it's easy to modify a suggestion provider to make it case insensitive.

The first step is to ensure you are comparing suggestions with the user input in a case insensitive way. The easiest way to do this is by converting both the user input and the possible suggestion to lowercase using the toLowerCase() method and then comparing these two values. If there's a match, then it's time to add the suggestion to the array of possible values.

When a suggestion is made, the first characters are always the ones that the user has typed, so you need to be sure not to lose that. It would be very distracting to a user if he or she typed "mIs" and it was changed to "Mis." Even though the person is typing the state name "Missouri", the suggestion should be "mIssouri." Since there's no way of knowing what case the user is going to be typing in, you're left combining what they just typed with the suggestions you have. To do this, take the user input and add to it the rest of the letters in the suggestion. For example, if the user typed "mIs," and one of the suggestions is "Missouri," you would combine "mIs" and "souri" to give a suggestion of "mIssouri." Here's the code:

StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl,
bTypeAhead) {
    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;

    if (sTextboxValue.length > 0){

        var sTextboxValueLC = sTextboxValue.toLowerCase();

        for (var i=0; i < this.states.length; i++) {

            var sStateLC = this.states[i].toLowerCase();

            if (sStateLC.indexOf(sTextboxValueLC) == 0) {

                Suggestions.push(sTextboxValue + this.states[i].substring(sTextboxValue.length));
            }
        }
    }

    oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

As you can see, this code isn't very different from what was used in the previous article. The addition of case insensitive comparison and the creation of the suggestion only adds a couple of lines of code. You can check out the example to test this functionality.

Remote Suggestions

There's a lot of talk these days about JavaScript remoting, which involves using JavaScript to send information back to the server and then receiving information back. This technique centers around a Microsoft-proprietary object called XMLHttpRequest. Since this object became so widely used by Internet Explorer developers, other browsers have begun to implement it as well. Mozilla-based browsers, such as Firefox, support the object in the exact same way as Internet Explorer. Both Opera and Safari also introduced the XMLHttpRequest object in their most recent releases, though their implementations are a bit buggy (these should be fixed soon). This is the primary technology you will be using in this section.


home / programming / javascript / ncz / column3 / 1 current pageTo page 2To page 3
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27, 2003
Revised: May 30, 2005

URL: http://webreference.com/programming/javascript/ncz/column3/1