With this script I fill the dropdownbox dynamicly:
function loadDropbox(boxName,checkBox,selected){
var htmlStr="";
var searchXML = new ActiveXObject("Msxml2.DOMDocument");
searchXML = getXML("WEBDRPO0;?BDR="+bdr+"&TYPE_ART=D");
var nodeList = xmlDoc1.documentElement.selectNodes("//box[@name='"+boxName+"']");
try{
for(i=0;i<nodeList.length;i++){
var vals = ""+nodeList[i].firstChild.nodeValue+"";
vals = vals.split("~");
for(i=0;i<vals.length;i++){
if(checkBox=='1'){
htmlStr+="<br /><label><input type=checkbox name='aanwezigheid' id='aanwezigheid' value="+vals[i]+" id="+vals[i]+"/>"+vals[i]+"</label><br/>";
}else if(selected == vals[i]){
htmlStr+="<option selected='selected'>"+vals[i]+"</option>";
}else{
htmlStr+="<option>"+vals[i]+"</option>";
}
}
}
}
catch(e){}
return htmlStr;
}
This is the HTML result:
<select id="year" name="year">
<option>--Make Choice--</option>
<option>1980 - 1990</option>
<option>1990 - 2000</option>
<option>2000 - 2010</option>
<option>2010 - later</option>
</select>
With this Javascript I would make the "2010 - later" value the seleceted value.
<script language="javascript" type="text/javascript">
document.getElementById("year").value = "2010 - later";
</script>
It works fine in Firefox, but it don't work in IE9. What am I doing wrong?
Regards,
Freexel
2 Answers 2
You should set value attribute for the option.
Eg:
<option value="2010 - later">2010 - later</option>
Update: According your code, it should be:
htmlStr+='<option value="'+vals[i]+'" selected="selected">'+vals[i]+'</option>';
And
htmlStr+='<option value="'+vals[i]+'">'+vals[i]+'</option>';
answered Feb 11, 2012 at 12:23
xdazz
161k38 gold badges255 silver badges278 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Freexel
Thank you, but this is static, the selected value is different each time. That's the reason why I must use javascript.
xdazz
Why this is static, I mean when you create the select, you should set the value attribute for the option, no matter it is static or dynamic.
Freexel
I have update my question :) Maybe you understand my question better now :)
I believe this is what you want...
Working in FF and IE both...
<html>
<body>
<select id="year" name="year">
<option>--Make Choice--</option>
<option selected value="1980 - 1990">1980 - 1990</option>
<option value="1990 - 2000">1990 - 2000</option>
<option value="2000 - 2010">2000 - 2010</option>
<option value="2010 - later">2010 - later</option>
</select>
</body>
<script language="javascript" type="text/javascript">
document.getElementById("year").value = "2010 - later";
</script>
</html>
Also your htmlStr+="<option>"+vals[i]+"</option>"; should not be htmlStr+="<option value='" +vals[i]+ "'>"+vals[i]+"</option>";
Good Luck
answered Feb 11, 2012 at 12:20
Fahim Parkar
31.8k46 gold badges169 silver badges285 bronze badges
1 Comment
Freexel
Thank you, I know that. But I must use Javascript, because I'm filling the box dynamicly, using XML.
default