0

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

0

2 Answers 2

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>';
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but this is static, the selected value is different each time. That's the reason why I must use javascript.
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.
I have update my question :) Maybe you understand my question better now :)
0

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

1 Comment

Thank you, I know that. But I must use Javascript, because I'm filling the box dynamicly, using XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.