Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sarching through names inside dropdown box.

Status
Not open for further replies.

sharapov

MIS
May 28, 2002
106
0
0
US
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?

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(&quot;document.frmTest.drpTest.selectedIndex = i&quot;, 50);
                      matched = true;
                      break;
                  }
              }
              
               if (!matched) {
                  testString = testString.replace(/.*(.)$/, &quot;$1&quot;);
              }
          
           }

The form looks something like this:

Code:
<form name=&quot;frmTest&quot;>
<select name=&quot;drpTest&quot; id=&quot;drpTest&quot; onkeydown=&quot;addChar(event);&quot;>
<option selected>Choose Recipient...</option>
<option> . . .</option>
</select>
</form>
 
put this

function addChar(e) {
if (e.keyCode >=65 && e.keyCode <=122){
if (e.keyCode) {
code = e.keyCode;
} else if (e.which) {
code = e.which;
}

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

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

var matched = false;

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

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

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
mwolf00,

Thank you for your help.
It works fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top