javascript thread: lookup through a select box options, and not by forst leter only
This code will do it for you:
<html>
<head>
<title>Find name</title>
<script language=3D"JavaScript" type=3D"text/javascript">
<!--
var arrAllNamesOptions =3D new Array();
window.onload =3D function (){
document.forms["theForm"].elements["inputField"].focus();
arrAllNamesOptions =3D
document.forms["theForm"].elements["allNamesSelect"].options;
}
function findName(searchText){
for(var i=3D0; i<arrAllNamesOptions.length; i++){
if(arrAllNamesOptions[i].text.toLowerCase().indexOf(searchText.toLowerCas
e()) =3D=3D 0){
arrAllNamesOptions[i].selected =3D true;
break;
}
else arrAllNamesOptions[0].selected =3D true;
}
} =09
//-->
</script>
</head>
<body>
<div align=3D"center">
<form name=3D"theForm" onSubmit=3D"return false">
<table border=3D"0" cellspacing=3D"2" cellpadding=3D"2">
<tr>
<td><input type=3D"text" name=3D"inputField" value=3D"" size=3D"50"
onKeyUp=3D"findName(this.value)"/></td>
</tr>
<tr>
<td>
<select id=3D"allNamesSelect" size=3D"10">
<option> No match found </option>
<option>Anders</option>
<option>Bj=F6rn</option>
<option>Dan</option>
<option>Daniel</option>
<option>Erik</option>
<option>Gunnar</option>
<option>Johanna</option> =09
<option>Per</option>
<option>Robert</option> =09
<option>Sara</option>
<option>Tony</option>
<option>Wictor</option>
</select>
</td>
</tr>
</table> =09
</form>
</div>
</body>
</html>
/Robert
-----Original Message-----
From: avivits@s... [mailto:avivits@s...]
Sent: den 30 januari 2003 14:11
To: javascript
Subject: [javascript] lookup through a select box options, and not by
forst leter only
Hi,
I'd like to know if possible to scroll to a specific value in select
box,
by typing the what needed while focus is on the select box. I.e. - I
have a list of authors in a select-box (was built in asp), and
would like a javascript(!) code for the select box to scroll to
option "Smith" when I am typing "smit" or "smith" etc.
And would like to scroll to author "Smirnof" when typing "smir", "smirn"
etc.
I do NOT want to have an extra text field for that, and NOT having a
"go"
button as well, but would like this to scroll to the right option in
list
(only scrolling), while typing in the very same select box.
For now, built in in IE, it scrolls to the first author that starts with
the one letter I type. I'D like the same but for a string, not one
letter.
If possible pls help, or let me know where I can see code samples of
such
things.
Thanks very much