I am using the following code to search throught 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 elemenate it?
The form looks something like this:
Code:
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:
Code:
<form name="frmTest">
<select name="drpTest" id="drpTest" onkeydown="addChar(event);">
<option selected>Choose Recipient...</option>
<option> . . .</option>
</select>
</form>