Hi,
I'm trying to write some code that will search through a select box
ie, when a select box is selected, then the user starts typing, it will jump down to an option that matches that text
Here's the code i have at the moment:
<html>
<script>
function something(e){
thisForm = document.test;
thisKey = String.fromCharCode(event.
keyCode);
if(e){ // if the event object is present (NN only)
e = e // var e = event
}
else {
e = window.event // else e = winddow.event for IE
}
if(e.which){ // if there is syntax support for the property 'which' (NN only)
var keycode = e.which // e.which is stored in variable "keycode"
}
else {
var keycode = e.keyCode // otherwise for IE, var keycode stores e.keyCode syntax
}
if(keycode==8){ // keycode for backspace is 8, so if backspace is pressed,
if(thisForm.searchtext.val
ue.length != 0){
thisForm.searchtext.value = thisForm.searchtext.value.
substr(0, thisForm.searchtext.value.
length-1);
}
}
else if(thisKey.match(/^[A-Za-z
]+$/)){
thisForm.searchtext.value = thisForm.searchtext.value + String.fromCharCode(event.
keyCode);
findMatch(thisForm.mySelec
t, thisForm.searchtext.value,
e);
}
}
function findMatch(selectBox, txtFind, objEvent) {
var i;
var s = "";
var st2 = new String(txtFind);
s2 = st2.toLowerCase();
for (i=0; i<selectBox.options.length
; i++) {
s = selectBox.options[i].text;
st = new String(s);
s = st.toLowerCase();
if (s.indexOf(s2) == 0) {
selectBox.selectedIndex = i-1;
return;
}
}
}
</script>
<body>
<form name="test">
<input type="hidden" name="searchtext" value="">
<select name="mySelect" onKeyDown="something(event
);">
<option value = "">-- Select Someone --</option>
<option value = "">Bob Smith</option>
<option value = "">Bob Someone</option>
<option value = "">Joe Blow</option>
<option value = "">Your Momma</option>
<option value = "">Nomar Blah</option>
<option value = "">Oma</option>
</select>
<p>
<input type="button" onclick="alert(document.te
st.searcht
ext.value)
" value="test">
</form>
</body>
</html>
It almost works, the keys are recorded in the hidden field. When you type b, it jumps down to the first bob, as it normally would, but then you type o and it jumps down to oma, even though the hidden field now has "bo" in it.
Can anyone help?
Start Free Trial