spacer

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

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

Sr. Programmer/Analyst
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume
Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next

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]



JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
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
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider

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

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