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!

Dynamically populate options in a select

Status
Not open for further replies.

tackaman

IS-IT--Management
May 24, 2011
5
US
I need help with code that auto-populates the options in a select. I have a group of about 30 check boxes that i want to fire a subroutine when clicked. That sub needs to loop through all of those checkboxes and if it has a specific class and is checked then the values of those checkboxes need to be added as options to 6 different selects.

If you uncheck a box then it needs to clear out the selects and repopulate again with the new values of all of the boxes checked.

Can anyone help?
 
I would get all checkbox, see if they are checked, build the option in a string and then modify the content of the select element usings .innerHTML. For instance

untested and conceptual
Code:
<script language="vbscript">
   sub changeOptions()
      set objCheckBoxes = document.getElementsByTagName("input")
      set objSelect = document.getElementById("names")
      set objOptions = objSelect.options
	  
      strHTML = ""
      for each objElement in objCheckboxes
         if (objElement.checked) then
            strHTML = strHTML & "<option value="" " & objElement.value & " "">" & objElement.value & "</option>"
         end if
      next
      
      objSelect.innerHTML = strHTML
   end sub
</script>     

<input type="checkbox" value="Bob" onClick="changeOptions()"> Bob
<input type="checkbox" value="Mary" onClick="changeOptions()"> Mary 
<input type="checkbox" value="Pat" onClick="changeOptions()"> Pat

<select id="names"></select>

-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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top