Click to See Complete Forum and Search --> : Sarching through names inside dropdown box.


sharapov
11-20-2003, 12:02 PM
I am using the following code to search thought the "user's names" in the dropdown box. In other words when I start typing user name it automatically bring it up. Everything works fine, but if I press on the up or down arrow on the keyboard while dropdown box selected, the script shows an error. Can anybody help me to eliminate it?



var testString = "";

function addChar(e) {

if (e.keyCode) {
code = e.keyCode;
} else if (e.which) {
code = e.which;
}

if (code == 8) {
testString = testString.replace(/.$/, "");
} else {
testString += String.fromCharCode(code);
}

re = new RegExp("^" + testString, "i");

var matched = false;

for (i = 0; i < document.frmTest.drpTest.options.length; i++) {
if (document.frmTest.drpTest.options[i].text.match(re)) {
setTimeout("document.frmTest.drpTest.selectedIndex = i", 50);
matched = true;
break;
}
}

if (!matched) {
testString = testString.replace(/.*(.)$/, "$1");
}

}



The form looks something like this:



<form name="frmTest">
<select name="drpTest" id="drpTest" onkeydown="addChar(event);">
<option selected>Choose Recipient...</option>
<option> . . .</option>
</select>
</form>