Click here to Skip to main content
Licence 
First Posted 17 Sep 2007
Views 15,160
Downloads 100
Bookmarked 13 times

Auto Suggest Auto Complition

By | 17 Sep 2007 | Article
Auto suggest and Auto complition of your textbox.

Introduction

I occasionally run into situations where it would be really great to add an feature to a text input on a web form. That is, something along the lines of Auto Complete

Background

There are a bunch of scripts available on the Internets to handle this for you, but they all do things their own way, and they're never exactly what yor want. I'm always running into the same problems with them

So, I finally broke down and wrote my own script. It may not be better than what's out there, but hopefully it's easier to understand.

  • The usual situation is that I want to customize some element of the script's behavior, but the code is so dense and obtuse that I can't make changes without breaking things.
  • No documentation. A big hairy mass of code is even harder to deal with if you don't know what it's supposed to do in the first place.
It seems to work pretty well for me, so I've given it a super-snazzy name, and wrapped it up with some doc in a zip file for you guys. Just to make it official

Using the code

just put this javascript an add one line in you page like this
<script type="text/javascript">
    window.onload = function () {
    var oTextbox = new AutoSuggestControl(document.getElementById("txt1"), new StateSuggestions());        
     }
</script>
   

matches criteria are set

currently it is not set.but you can set it here

StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) 
{
    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;
    
    if (sTextboxValue.length > 0)
    {    
        //search for matching states
        for (var i=0; i < this.states.length; i++) 
        { 
            if (this.states[i].toLowerCase().indexOf(sTextboxValue.toLowerCase()) == 0) 
            {
                aSuggestions.push(this.states[i]);
            } 
        }
    }

    //provide suggestions to the control
    oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

in the for loop you can set your criteria it may be uppercase or lowercase

Points of Interest

if you are using html controlthen no prblem but you are using dotnet textbox control so

<asp:TextBox runat="server" ID="txt_City" AutoCompleteType="Disabled"></asp:TextBox>
 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chirag Patadia



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPoor Quality PinmemberKirill Osipov9:20 28 May '09  
GeneralA little work needed in AutoSuggestControl.prototype.handleKeyDown Pinmemberfwsouthern5:18 17 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 Sep 2007
Article Copyright 2007 by Chirag Patadia
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid