JavaScript
  Home arrow JavaScript arrow Page 3 - Suggest As You Type
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Suggest As You Type
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2004-04-19

    Table of Contents:
  • Suggest As You Type
  • The Plan
  • The Suggestion Function
  • The End

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Suggest As You Type - The Suggestion Function


    (Page 3 of 4 )

    Ok, now that we have that clear in our minds, let's see how to do the actual suggestion function.


    <script language="javascript">
    function suggestName
    level 
    {
     
    if ( isNaNlevel ) ) { level 
    }

    This is just some simple error checking. This prevents run-time errors in case the function somehow gets called in an incorrect fashion (with a parameter other than an integer). Also, you'll notice that when we call it from the text box, we're not supplying any parameter at all, so 'level' gets initialized to 1. As we'll see in a second, the 'level' just helps us call the function recursively until we've matched all of the letters possible.


     var document.form1;
     
    var listbox f.employeeList;
     
    var textbox f.employeeName;

    These are more or less the only three variables that we need to modify to just pop this script into any page we have and use it. The names are fairly self-explanatory, so I'll move on.


     var soFar textbox.value.toString();
     
    var soFarLeft soFar.substring(0,level).toLowerCase();
     
    var matched false;
     
    var suggestion '';

    What happened here, is that we're taking what's been typed into the text box, and taking a chunk of it to the length of 'level'. This means that if you paste a six letter word into the text box, the function will first start by only matching the first letter, then the second, and so on. This is important, because it means that you'll always see the closest match, even if there is no exact match for the letters you've typed. Now let's get into the search mechanism.


     for ( var 0listbox.lengthm++ ) {
      suggestion 
    listbox.options[m].text.toString();
      suggestion 
    suggestion.substring(0,level).toLowerCase();
      
    if ( soFarLeft == suggestion ) {
       listbox
    .options[m].selected true;
       matched 
    true;
       
    break;
      
    }
     
    }

    So what this chunk of code does, is iterate through the list, grabbing the portion of each item, left-trimming it to the size of 'level'. If it's matched, we set that item to be selected, trip the boolean 'matched' flag, and break out of the loop. If no match is found after going through the entire list, the function exits, and we're left with the last selected item, the one closest to what we've typed. If a match is found however, we run through the function again, this time incrementing the level first, to see if we can perhaps find an ever closer match. We can do that like this:


     if ( matched && level soFar.length ) { level++; suggestName(level) }
    }
    </script>


    And that's it, that's the wonderful 'suggest as you type' function that I use over and over in my forms.

    More JavaScript Articles
    More By Justin Cook


     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT