Click here to Skip to main content
12,952,792 members (35,209 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 24 Feb 2007

Select As You Type Behavior in Internet Explorer Comboboxes

, 24 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Makes ordinary Internet Explorer comboboxes select as the user types


Selecting elements from a browser combobox with a lot of elements is a fairly common, yet annoying task, primarily because of the lack of select-as-you-type behavior. Atleast in Internet Explorer, each keypress selects the first element in the combobox starting with the key pressed, so typing in the first two letters of the element to select will not work as expected. This article provides a short piece of JavaScript code that you can use to provide the select-as-you-type behavior, with very few modifications.

How It Works

The basic idea is to capture and append keypresses on a combobox and then look for elements in the combobox starting with the list of keys pressed by the user. Here is the code in its entirety:

var keysPressed = [];

function selectAsYouType(e)
    var keyChar; var keyNum;
    var comboBox;
    if (window.event) // Internet Explorer
        keyNum = e.keyCode;            
        return true; // browser doesn't support DOM, do default processing.
    keyChar = String.fromCharCode(keyNum);
    comboBox = e.srcElement;
    if (!comboBox)
        return true; // if DOM didn't work, do default processing
    if (keyNum == 27) // Esc key
        return selectElement(comboBox, keyChar);
    return true;

function clearKeysPressed(e)
    if (e.srcElement)

function selectElement(comboBox, keyChar)
    var i = 0;
    var keysPressedSoFar =     keysPressed[comboBox] || "";
    keysPressedSoFar += keyChar;
    keysPressed[comboBox] = keysPressedSoFar;
    var options = comboBox.options;
    for (i = 0; i<options.length; ++i)
        if (options[i].text.toUpperCase().indexOf
                                          (keysPressedSoFar.toUpperCase(), 0) == 0)
            comboBox.selectedIndex = i;
            return false; // don't do default processing
    // didn't match start of any option, so reset keysPressed to just this key
    keysPressed[comboBox] = String(keyChar); 
    return true;        

keysPressed is a global array that stores the keysPressed for each combobox in the page. Each combobox that needs the select-as-you-type behavior would need to add two handlers:

  1. onkeypress="return selectAsYouType(event)" - This captures keypresses on the combobox
  2. onblur="clearKeysPressed(event)" - This clears off the keyboard cache (for that combo<code>box) when focus moves out of the combobox

For example:

<select onkeypress="return selectA(event)" onblur="clearKeysPressed(event)">

The code is fairly straightforward. The selectElement function takes the current key pressed, appends it to the keys pressed so far and checks if any element in the combobox starts with a string made up of keysPressedSoFar. If there is no match, it returns true to revert to the default behavior of the browser.

Points of Interest

A possible improvement would be to use a better algorithm to search for elements starting with a particular string. This code has been tested with comboboxes containing ~2800 elements (Click here for an example) without any performance problems.


  • Initial post - 12:11 PM 2/24/2007


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

S. Senthil Kumar
Software Developer Atmel R&D India Pvt. Ltd.
India India
I'm a 27 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai. I'm currently working in C# and C++, but I've done some Java programming as well. I was a Microsoft MVP in Visual C# from 2007 to 2009.

You can read My Blog here. I've also done some open source software - please visit my website to know more.

You may also be interested in...


Comments and Discussions

GeneralSelection reset Pin
Borki28-Feb-07 8:13
memberBorki28-Feb-07 8:13 
GeneralRe: Selection reset Pin
S. Senthil Kumar1-Mar-07 6:36
memberS. Senthil Kumar1-Mar-07 6:36 
GeneralSeems to be working everywhere... Pin
sameeraperera24-Feb-07 15:24
membersameeraperera24-Feb-07 15:24 
GeneralRe: Seems to be working everywhere... Pin
S. Senthil Kumar25-Feb-07 5:26
memberS. Senthil Kumar25-Feb-07 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 24 Feb 2007
Article Copyright 2007 by S. Senthil Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid