Click to See Complete Forum and Search --> : searching drop down box


webtekie
07-21-2003, 08:07 AM
Hello,

Is there a way to search a drop down box in HTML similar to what one can do in Java Swing? I wrote this class in Java Swing that allows someone to enter a string into a drop down box field, press enter and drop down box automatically sets the focus of drop down box on item searched for. Is there a way to do same thing using JavaScript?

thanks,
webtekie

Nevermore
07-21-2003, 08:36 AM
I don't think you can do that, because HTML doesn't have Swing's combo boxes. However, you could have a textbox that you type the name into and it searches the drop down list.

webtekie
07-21-2003, 08:40 AM
sounds like a plan.
Thanks, I'll try it.

pyro
07-21-2003, 08:48 AM
Try this out:

<html>
<head>
<script type="text/javascript">
function searchForm(frm) {
for (i=0; i<frm.myselect.options.length; i++) {
re = new RegExp(frm.searchfield.value, "i");
if (frm.myselect.options[i].text.match(re)) {
frm.myselect.options.selectedIndex = i;
}
}
}
</script>
</head>
<body>
<form onsubm>
<input type="text" name="searchfield">
<input type="button" value="Submit" onclick="searchForm(this.form);"><br>
<select name="myselect">
<option value="1">One</option>
<option value="2">Two</option>
</select>
</form>
</body>
</html>