Click here to Skip to main content
Licence 
First Posted 20 Mar 2003
Views 127,164
Bookmarked 35 times

AutoSearch SELECT tag

By | 20 Mar 2003 | Article
Clientside JavaScript for AutoSearching HTML SELECT tags

Introduction

If you spend any time surfing the web you will find <select> lists. In all cases I've encountered the default single character searches are used to help you "quickly" find the list item you want. As an example, to reach North Carolina in a list of states you would type "N" which takes you to Nebraska. "O" will take you to Ohio, so you will quickly learn to type "N" and down arrow 6 times to get "North Carolina".

I think we can do better.

An auto searching <select> list would allow us to type "No" and automatically get "North Carolina". Mississippi and Arkansas are other good examples. A few lines of client side JavaScript can solve this nuisance. I've included examples of both types of <select> lists for USA states below so give them a try.

Standard Non Searching Select List

AutoSearching Select List

How it works

Basically the javascript code driving the AutoSearching <select> above records each keystroke in a hidden input tag and uses ALL keystrokes to search for a matching Option in the list. A few questions may come to mind, such as "How to you clear previously captured keystrokes?". The onBlur() and onFocus() events clear previous keystrokes but what about starting a new search without moving focus from the <select> list?. In this case I used the Delete key. I decided not to use BackSpace key for this since many browsers use BackSpace to navigate Back.

How to use AutoSearch <select>

First, add selectKeyDown(), selectKeyPress(), and clr() javascript functions to your page as found in the source code download, or simply View->Source for this page.

Next, add onBlur(), onFocus(), onKeyPress(), and onKeyDown() event handlers to your <select> tags

Last, add a hidden input tag named "keys" to store the previous keystrokes

A little debugging in case you are an occasional fumble finger typist like me and your <select> tags will never be the same.

The Code

// Java Script to Handle AutoSearch
function selectKeyDown()
{
    // Delete Key resets previous search keys
    if(window.event.keyCode == 46)
        clr();
}
function selectKeyPress()
{
    // Notes:
    //    1) previous keys are cleared onBlur/onFocus and with Delete key
    //    2) if the search doesn't find a match, this returns to normal 1 key 
    //        search setting returnValue = false below for ALL cases will 
    //        prevent default behavior
    
    //TODO:
    //    1) add Netscape handling
    

    var sndr = window.event.srcElement;
    var pre = this.document.all["keys"].value;
    var key = window.event.keyCode;
    var char = String.fromCharCode(key);

    // "i" -> ignoreCase
    var re = new RegExp("^" + pre + char, "i"); 

    for(var i=0; i<sndr.options.length; i++)
    {
        if(re.test(sndr.options[i].text))
        {
            sndr.options[i].selected=true;
            document.all["keys"].value += char;
            window.event.returnValue = false;
            break;
        }
    }
}
function clr()
{
    document.all["keys"].value = "";
}

Tested Browsers

  1. Internet Explorer 6.0.2800
  2. Mozilla 1.2.1

TODO

  1. Add Netscape Navigator handling.
  2. Test other and older browsers.
  3. Let me here your comments on this.

History

Release 03/02/2003

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

Oscar Bowyer

Web Developer

United States United States

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
GeneralCool But not in IE PinmemberJanaka Lakmal4:30 27 Nov '07  
This is not working in IE.
 
Thanks,
Janaka
 
Novice to programming

Questiona search technique with a textbox and a listbox Pinmemberkamalika_kk2:46 19 Jul '06  
Generalplz ignore this message PinmemberSasan1:27 3 Dec '05  
GeneralSo Cool~ PinmemberHyosik, Kim22:02 7 Jul '05  
GeneralSearching in a drop-down PinsussGibran Castillo7:42 24 May '05  
GeneralRe: Searching in a drop-down PinsussAnonymous12:29 26 May '05  
GeneralTest for expanded select PinmemberEricRapp11:48 19 May '05  
AnswerRe: Test for expanded select PinmemberBandu Patil23:56 11 Mar '06  
GeneralGood Job done Pinsussanup2anup3:17 20 Jan '05  
GeneralVery good Pinmemberfelipedr0:16 6 Jan '05  
Generalnice trick PinmemberChristian Kiefer5:00 30 Apr '04  
Generalbackspace PinmemberFocusQueen4:43 23 Jan '04  
AnswerRe: backspace PinmemberBandu Patil22:17 14 Aug '04  
GeneralCan't get OnChange() event Pinsussrutvij8:26 20 Jan '04  
GeneralRe: Can't get OnChange() event Pinsussnprash4:17 16 Aug '04  
Generalanother quick mod PinsussAnonymous7:26 14 Nov '03  
GeneralMy modified version PinsussPiedPiper697116:45 23 Oct '03  
GeneralCool, but.. Pinmemberlaminator5:32 10 Nov '03  
GeneralRe: My modified version Pinsussanup agrawal3:26 20 Jan '05  
GeneralRe: My modified version Pinsussanup agrawal3:26 20 Jan '05  
GeneralRe: My modified version PinmemberJason Robbins5:01 21 Jul '05  
GeneralCool, but hidden field is unnecessary Pinmembersilentdoug12:37 22 Jul '03  
GeneralRe: Cool, but hidden field is unnecessary Pinmemberplegg6:29 27 Aug '03  
GeneralVery good. EOL Pinmember.S.Rod.23:11 21 Mar '03  
GeneralCool PinmemberSteve McLenithan11:05 21 Mar '03  

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
Web02 | 2.5.120529.1 | Last Updated 21 Mar 2003
Article Copyright 2003 by Oscar Bowyer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid