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!

Selecting listbox option from textbox entry 1

Status
Not open for further replies.

JKehoe

Programmer
Feb 8, 2001
17
0
0
US
Hello,

I have a textbox and a listbox on a form. The listbox contains a few hundred names in alphabetical order. I would like to link the textbox with the listbox, so that when a user starts typing in the textbox, the closest match will be selected in the listbox. I've got an "onkeyup" event on the textbox that is trapping the entered text... does anyone know how to use this to select the closest option in the listbox?

Thanks in advance,
jt
 
I put in this block:

Code:
if (blah == '') {
   blahForm.blahSelect.options[0].selected = true;
}

just to default to the first position in the pulldown. Otherwise, if a person backspaces out what they had typed, it defaults to the last position in the pulldown. And sorry about all the blah's..... it's always just the first thing that comes to my head.

Code:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
<SCRIPT LANGUAGE=JavaScript>

function blahSmart(blah) {
   len = blah.length;
   size = blahForm.blahSelect.options.length;
   for (i = 0; i < size; i++) {
      if (blah == '') {
         blahForm.blahSelect.options[0].selected = true;
      }
      if (blah == blahForm.blahSelect.options[i].value.substring(0, len)) {
         blahForm.blahSelect.options[i].selected = true;
         return true;
      }
   }   
}

</SCRIPT>

<BODY>
<FORM NAME=&quot;blahForm&quot;>

<select name=&quot;blahSelect&quot;>
<option value=&quot;&quot; selected>Try Me Out</option>
<option value=&quot;ape&quot;>ape</option>
<option value=&quot;asp&quot;>asp</option>
<option value=&quot;bag&quot;>bag</option>
<option value=&quot;bat&quot;>bat</option>
<option value=&quot;cat&quot;>cat</option>
<option value=&quot;dawg&quot;>dawg</option>
<option value=&quot;dog&quot;>dog</option>
<option value=&quot;xxx&quot;>xxx</option>
<option value=&quot;yo&quot;>yo</option>
</select>
<br>
<input type=text name=blahText onkeyup='javascript:blahSmart(this.value)'>

</FORM>
</BODY>
</HTML>

Take note that the items have to appear in the pulldown in alphabetical order in order for them to alphabetically appear when typing.

-kaht
 
This works perfectly! Thanks so much! BLAH!!!!!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top