View Single Post
Old 03-21-2011, 03:26 AM   PM User | #1
supersav144
New Coder

 
Join Date: Dec 2010
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
supersav144 is an unknown quantity at this point
Adding Options to A Selectbox

I currently have two selectbox, the first for categories and the second for subcategories. When an option from the category selectbox is chosen I would like the subcategory selectbox to be filled with options drawn from an array.

The following code I have works for Chrome and Firefox but not in IE, I would appreciate any help to fix the code or new code which does a similar function.

HTML:
Code:
<select size="9" id="category" name="category" onclick="return categoryChange();">
                    	<option>Antiques ></option>
                        <option>Art and Crafts ></option>
                    </select>

                    <select style="visibility:hidden" id="subcategory" name="subcategory" size="9">
                    </select>
JAVASCRIPT:

Code:
function addOption(selectbox,text,value){
			var optn = document.createElement("OPTION");
			optn.text = text;
			optn.value = value;
			selectbox.options.add(optn);
		}
		
		var antiques = new Array("Furniture","Ceramics","Glass");
		var art = new Array("Drawings and Paintings","Photographs");
		
		
		function categoryChange(){
			
			document.getElementById("subcategory").style.visibility = "visible";
			document.getElementById("subcategory").innerHTML = "";
			
			
			if(document.getElementById("category").value == "Antiques >"){
				for (var i=0; i < antiques.length;++i){
					addOption(document.myForm.subcategory, antiques[i], antiques[i]);
				}		
			}
			else if(document.getElementById("category").value == "Art and Crafts >"){
				for (var i=0; i < art.length;++i){
					addOption(document.myForm.subcategory, art[i], art[i]);
				}
			}
                }
supersav144 is offline   Reply With Quote