It doesn't work with an else statement :-(
Posted: Wed Dec 01, 2004 7:13 pm
I wrote a function that updates a drop box if a user types a string in a text box matching a value in an option in the drop box.
This worked. Then I tried to adjust it so that it sits on option[0] if the string typed doesn't match a value but now all it ever does is go to option[0].
Here's what I mean. In this example if the user types the string "123" in the text box the the drop box changes it's selected index to 1 etc.
The function:
And here's the implementation of it:
I made the loop run from index 1 onwards since index 0 is blank so that shouldn't be the problem. Without the else statement it will update over and over if a keyed string matches a value but with that else statement the first keypress causes the box to change the selected index to zero and never move again even if a subsequent string is matched.
Any ideas?
Thanks
This worked. Then I tried to adjust it so that it sits on option[0] if the string typed doesn't match a value but now all it ever does is go to option[0].
Here's what I mean. In this example if the user types the string "123" in the text box the the drop box changes it's selected index to 1 etc.
The function:
Code: Select all
function updateDrop(el1,el2) {
for(i=1; i<el2.options.length; i++) {
if (el1.value == el2.optionsїi].value) {
el2.options.selectedIndex = i
} else {
el2.options.selectedIndex = 0
}
}
}Code: Select all
<form action="demo" method="post" name="demo">
<input type="text" id="text1" name="text1" onKeyup="updateDrop(this,document.getElementById('drop1'))">
<br>
<select name="drop1" id="drop1">
<option value="">Numbers</option>
<option value="123" selected>123</option>
<option value="123456">123456</option>
<option value="45678">45678</option>
<option value="5623">5623</option>
</select>
</form>Any ideas?
Thanks