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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdown?

Status
Not open for further replies.

nmath

Programmer
Dec 12, 2003
47
US
I have a question. Does anyone know of a way to create a select box that works like a browser history list. For example, while the user types the box is being searched, if the value is in the select box then nothing is added, but if the value does not exist then the value is added to the select box. Thanks in advance!
 
Try this...

<script>
function findIt(){
typedVal = document.myForm.myText.value + String.fromCharCode(window.event.keyCode)
window.status = typedVal
theSelect = document.getElementById(&quot;mySelect&quot;)
for (x=0; x<theSelect.options.length; x++){
if(typedVal.toLowerCase() == theSelect.options[x].value.toLowerCase().substr(0,typedVal.length)){
theSelect.selectedIndex = x
}
}
}
</script>

<form name=&quot;myForm&quot;>
<input name=&quot;myText&quot; onKeyPress=&quot;findIt()&quot;>
<select id=&quot;mySelect&quot;>
<option value=&quot;&quot;>
<option value=&quot;Alabama&quot;>Alabama
<option value=&quot;Alaska&quot;>Alaska
<option value=&quot;Maryland&quot;>Maryland
<option value=&quot;Minnesota&quot;>Minnesota
<option value=&quot;Missouri&quot;>Missouri
</select>
</form>

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)

 
Thanks for post! I worked on the problem for a little while and came up with close to the same solution. Here was my solution if anyone is interested

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

for(i = 0; i<sellength; i++)
{
if (Mainform.wholetext.options.text.toLowerCase().indexOf(mytext.toLowerCase(),0) == 0)
{Mainform.wholetext.options.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 = &quot;Mainform&quot;>
<input type = &quot;text&quot; name = &quot;mytext&quot; onkeyup = &quot;fillin()&quot;>

<select name = &quot;wholetext&quot; onclick = &quot;filltext()&quot;>
<option>Red</option>
<option>Red Flowers</option>
<option>Green</option>
<option>Grass</option>
</select>
</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top