INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

form elements

How to create an autocomplete select box by nmath
Posted: 5 Feb 04 (Edited 23 Apr 04)

I wanted to create a text box that allowed a user to start typing a word and if it was in the select box it would auto-complete.  This is the code that I used to solve the problem.  There is a text box that as the user inputs characters it searches the select box for matches, also if the user selects a value from the select box the text box is auto filled.

CODE

<script language="javascript">
function fillin()
{
var mytext = Mainform.mytext.value;
sellength = Mainform.wholetext.length;
var i;

for(i = 0; i<sellength; i++)
{
   if (Mainform.wholetext.options[i].text.toLowerCase().indexOf(mytext.toLowerCase(),0) == 0)
     {Mainform.wholetext.options[i].selected = true;
     break;}
}

}

function filltext()
{
var selectedItem = Mainform.wholetext.selectedIndex;
var selectedText = Mainform.wholetext.options[selectedItem].text;

Mainform.mytext.value = selectedText;
}
</script>

Here is the HTML section:
<form name = "Mainform">
<input type = "text" name = &quot;mytext&quot; onkeyup = "fillin()">

<select name = "wholetext" onclick = "filltext()">
 <option>Red</option>
 <option>Red Flowers</option>
 <option>Green</option>
 <option>Grass</option>
</select>
</form>

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close