Click here to Skip to main content
6,630,901 members and growing! (18,704 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Autocomplete edit control for Javascript

By zichun

Shows how to implement a Javascript based autocomplete edit control
Javascript, Dev
Posted:21 Aug 2003
Views:111,211
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 4.42 Rating: 3.46 out of 5
4 votes, 21.1%
1
3 votes, 15.8%
2
1 vote, 5.3%
3
2 votes, 10.5%
4
9 votes, 47.4%
5

Introduction

This is a HTML control which integrates autocomplete into the standard textbox. By adding a few lines of code, you can turn any standard textboxes into a user friendly and a rather powerful control.

How it works

Whenever the user types anything in the textbox, the onkeyup event is fired, and the script calls a function. The current text in the textbox is then compared lexically with an array of strings.

Javascript code

<script>
fruits = new Array('apple','pear','orange',
    'mango','durain','grapes','starfruit');
names = new Array('tom','dick','harry','john','petter','foo','bar');
function autocomplete(n,ac_array){
    if (n.value == "") return 0;
    if (event.keyCode == 8 && n.backspace){
        n.value = n.value.substr(0,n.value.length-1);
        n.backspace = false;
    }

    var r = n.createTextRange();    
    tmp= n.value;
    if (tmp == "")return 0;
    for (z=0;z<ac_array.length;z++){
        tmp2 = ac_array[z];
        count = 0;
        for (i = 0;i<tmp.length;i++){
            if (tmp2.charAt(i) == tmp.charAt(i)){
                count++
            }
        }
        if (count == tmp.length){
            diff = tmp2.length - tmp.length;
            if (diff <= 0) break;
            kap = "";
            for (i=0;i<tmp2.length;i++){
                if (i >= tmp.length) kap += tmp2.charAt(i);
            }
            n.backspace = true;
            r.text += kap;
            r.findText(kap,diff*-2);
            r.select();
            return 0;
        }
    }
    n.backspace = false;
    return 0;
}
</script>

<input name='fruit' type='text' class='textbox' title="Opening" 
    onkeyup="autocomplete(this,fruits)" size="20">
<input name='Name' type='text' class='textbox' title="Opening" 
    backspace='false' onkeyup="autocomplete(this,names)" size="20">

How to use the code

  1. You must define the array of strings which you want to have autocompleted.
    fruits = new Array('apple','pear','orange','mango','durain',
        'grapes','starfruit');
  2. Copy the entire autocomplete function and put it somewhere in your code.
  3. Add the event onkeyup='autocomplete(this,fruits)' in the textbox. Note that the 2nd argument should tally with the array name you defined at 1.
  4. Add the property backspace='false' in the textbox. The backspace property is applicable when a user press backspace in the textbox. It gets very irritating without the boolean; i.e. when the user press backspace, the highlighted characters get deleted, but it will automatically be replaced by the same characters due to the autocomplete.

Todo

  1. I've tested the autocomplete control with an array of size 150. The speed was pretty reasonable on my computer (P3 700Mhz). However, I'm sure the autocomplete function's string-matching algorithm can be further improved upon.
  2. Allow for case insensitive string-matching

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

zichun


Member
I'm young and cool Smile
Location: Singapore Singapore

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralMy vote of 2 Pinmembergopimayiru3:06 22 May '09  
GeneralNot working PinmemberMember 246109723:42 27 May '08  
QuestionAutocompletion PinmemberPratheesh Rajan3:17 4 Oct '07  
GeneralIs it possible to view all elements' list on particular character press Pinmemberanil22kar22:33 9 Sep '07  
GeneralProblem with 2 Edit Control !!! Pinmembersteeve.leprovost4:18 4 May '06  
Generalnew version Pinmemberdavidlars9910:32 21 Feb '06  
Generalmozilla Pinsussvadd__5:35 26 Feb '05  
GeneralMuch better... Pinmemberpackard7520:38 26 Oct '04  
GeneralRe: Much better... Pinmemberzichun22:35 27 Feb '05  
GeneralNot working with mozilla PinsussAnonymous7:41 24 Jun '04  
GeneralTo complete in the hole textrange.. PinsussAnonymous3:02 25 Feb '04  
GeneralError PinmemberEmelian18:34 17 Jan '04  
GeneralRe: Error Pinmemberzichun18:55 17 Jan '04  
GeneralRe: Error PinmemberEmelian19:35 17 Jan '04  
GeneralRe: Error Pinmemberzichun20:22 17 Jan '04  
GeneralMake Case Insensitive PinsussThe Silverlord0:16 5 Sep '03  
GeneralRe: Make Case Insensitive Pinmemberzichun6:35 5 Sep '03  
GeneralHow about a sever control? PinmemberPaul Haley13:07 29 Aug '03  
AnswerRe: How about a sever control? Pinmemberchenggn6:22 8 Apr '06  
GeneralAll OK, but ... PinmemberMandalay21:45 23 Aug '03  
GeneralRe: All OK, but ... Pinmemberzichun4:35 24 Aug '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Aug 2003
Editor: Nishant Sivakumar
Copyright 2003 by zichun
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project