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

Loop through options in a select

Status
Not open for further replies.

tackaman

IS-IT--Management
May 24, 2011
5
US
I have a subroutine that dynamically adds options to a select based on data entered into a number of text boxes. Everything works great except I need to change my sub to check the options that are in the select to see if it exists before adding the value. In other words, I don't want to duplicate the options in the select.

Can anyone provide sample code to loop through the options in a select and compare the values to the values in my text box?
 
I assume you are referring to a HTML select element?

HTML:
<select id="names">
	<option value="Bill">Bill</option>
	<option value="Jack">Jack</option>
	<option value="Mary">Mary</option>
</select>

Code:
set objOptions = document.getElementById("names")
for each objOption in objOptions
   msgbox objOption.value
next

Of course, this will only work when executed by IE. Non-IE browsers do not support VBS. If you need it to be cross-browser compatable, I recommend using javascript - the code is essentially the same.

- Geates





"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Or could it be a Select CASE structure?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top