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

searching for a string within a listbox.....

Status
Not open for further replies.

justme123

Programmer
Oct 9, 2003
14
0
0
US
hi all,
I have the foll. popup and I want help in writing a javascript function that will retrieve any values that matches the string in the list box(something like these - "3040" or "live"; 3040 is a code number that could be alphanumeric too).

typically, the items displayed in the list box look like this :
some code - description
100100000 - live animals

thanks...
 
1. do the values behind the dropdown match what's visible to the user?

2. is it possible that there will be any dashes '-' in the dropdown besides the one that seperates the code from the description? If not, this task will be quite easy using the .split("-") method

-kaht

banghead.gif
 
Hi kaht, thanks for your response....

answers to your questions:

1. yes
2. no other dashes

What is this .split method?...can you pl. shed some info on this....
thanks
 
Here's an example. Take note that this code assumes all values are split by " - " (space, dash, space).
Code:
<html>
<head>
<script language=JavaScript>
function searchList(str) {
   var found = false;
   for (i = 0; i < blahForm.blahSelect.options.length; i++) {
      if (blahForm.blahSelect.options[i].value != '') {
         var arr = blahForm.blahSelect.options[i].value.split(&quot; - &quot;);
         if (arr[0] == str || arr[1] == str) {
            blahForm.blahSelect.options[i].selected = true;
            found = true;
         }
      }
   }
   if (!found) {
      blahForm.blahSelect.options[0].selected = true;
   }
}
</script>
</head>
<body>
<form name=blahForm>
<input type=text name=blahText><br>
<input type=button name=blahButton value='Click Me' onclick='searchList(blahForm.blahText.value)'><br>
<select name=blahSelect>
<option value=''>No match</option>
<option value='1000 - blah1'>1000 - blah1</option>
<option value='2000 - blah2'>2000 - blah2</option>
<option value='3000 - blah3'>3000 - blah3</option>
<option value='4000 - blah4'>4000 - blah4</option>
<option value='5000 - blah5'>5000 - blah5</option>
</select>
</form>
</body>
</html>

-kaht

banghead.gif
 
thanks much kaht...

I wrote this function with the help of your input that i could use .split method and it works fine...

function searchForMatch(select, code) {
var typedValue = code.toUpperCase();
var found = false;
j=0;
for (var i=0; i < select.options.length; i++) {
if (select.options.text.toUpperCase().indexOf(typedValue) == 0 ||
select.options.text.toUpperCase().indexOf(&quot;- &quot; + typedValue) > 0) {
var opt = new Option(select.options.text,select.options.text);
document.forms[0].filteredValues.options[j]=opt;
j++
} else {
select.options.selected = false;
}
}
}

once again, i appreciate your reaponse...
 
Kaht,

I tried your example by modifying the <select size=3>, so when you type the keyword &quot;5000&quot; in the text box (then click), the selected item will be highlighted and scrollbar thumb will auto scroll down to that item. This works fine for most of the browsers except Konqueror, where that the scrollbar thumb will not scroll at all. Do you have any expereince on this matter? Thx in advance

plectrum
 
sorry, I've never even heard of that browser......

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top