469,360 Members | 2,099 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

options not loading in IE

Claus Mygind
571 512MB
The following code loads fine in fireFox but does not load in IE. The list box loads but the options in the list box fail to load

Expand|Select|Wrap|Line Numbers
  1. function writeMenu(cPage)
  2. {
  3. /anny8.html" onclick="goNext(\'/ABC/anny8.html\'); return false">An(n)y Geocaching</a>/&nbsp;'
  4.     if (cPage == "anny8")
  5.     {
  6.         document.getElementById("myMenu").innerHTML +=  '<select id="getNextGeoPage" onchange="navigate(this)" name="getNextGeoPage">;'
  7.         document.getElementById("myMenu").innerHTML +=  '</select>&nbsp;/'
  8.     }
  9.  
  10.     document.getElementById("myMenu").innerHTML +=  '<a href="/ABC/index.html" onclick="goNext(\'/ABC/index.html\'); return false">An(n)y Home</a>/&nbsp;'
  11.     document.getElementById("myMenu").innerHTML +=  '<a href="/SEC/testanny4.html" onclick="goNext(\'/SEC/testanny4.html\'); return false">An(n)y Testing</a>/&nbsp;'
  12.     if (cPage == "anny8")
  13.     {
  14.         document.getElementById("getNextGeoPage").innerHTML  = '<option value="" selected="selected">Select from archive</option>'
  15.         document.getElementById("getNextGeoPage").innerHTML += '<optgroup label="2009">'
  16.         document.getElementById("getNextGeoPage").innerHTML += '<option value="21">January</option>'
  17.         document.getElementById("getNextGeoPage").innerHTML += '</optgroup>'
  18.         document.getElementById("getNextGeoPage").innerHTML += '<optgroup label="2008">'
  19.         document.getElementById("getNextGeoPage").innerHTML += '<option value="18">December</option>'
  20.         document.getElementById("getNextGeoPage").innerHTML += '<option value="13">November</option>'
  21.         document.getElementById("getNextGeoPage").innerHTML += '<option value="7">October</option>'
  22.         document.getElementById("getNextGeoPage").innerHTML += '<option value="5">September</option>'
  23.         document.getElementById("getNextGeoPage").innerHTML += '<option value="1">August</option>'
  24.         document.getElementById("getNextGeoPage").innerHTML += '</optgroup>'
  25.     }
  26.  
  27. }
  28.  
Thinking it might have been a timing problem I also tried this method of loading the options. But it did not work either.
Expand|Select|Wrap|Line Numbers
  1. function writeMenu(cPage)
  2. {
  3. /anny8.html" onclick="goNext(\'/ABC/anny8.html\'); return false">An(n)y Geocaching</a>/&nbsp;'
  4.     if (cPage == "anny8")
  5.     {
  6.         document.getElementById("myMenu").innerHTML +=  '<select id="getNextGeoPage" onchange="navigate(this)" name="getNextGeoPage">;'
  7.         document.getElementById("myMenu").innerHTML +=  '</select>&nbsp;/'
  8.     }
  9.  
  10.     document.getElementById("myMenu").innerHTML +=  '<a href="/ABC/index.html" onclick="goNext(\'/ABC/index.html\'); return false">An(n)y Home</a>/&nbsp;'
  11.     document.getElementById("myMenu").innerHTML +=  '<a href="/SEC/testanny4.html" onclick="goNext(\'/SEC/testanny4.html\'); return false">An(n)y Testing</a>/&nbsp;'
  12.     if (cPage == "anny8")
  13.     {
  14.     setTimeout("loadListBox()", 0);
  15.     }
  16. }
  17. function loadListBox()
  18. {
  19.         document.getElementById("getNextGeoPage").innerHTML  = '<option value="" selected="selected">Select from archive</option>'
  20.         document.getElementById("getNextGeoPage").innerHTML += '<optgroup label="2009">'
  21.         document.getElementById("getNextGeoPage").innerHTML += '<option value="21">January</option>'
  22.         document.getElementById("getNextGeoPage").innerHTML += '</optgroup>'
  23.         document.getElementById("getNextGeoPage").innerHTML += '<optgroup label="2008">'
  24.         document.getElementById("getNextGeoPage").innerHTML += '<option value="18">December</option>'
  25.         document.getElementById("getNextGeoPage").innerHTML += '<option value="13">November</option>'
  26.         document.getElementById("getNextGeoPage").innerHTML += '<option value="7">October</option>'
  27.         document.getElementById("getNextGeoPage").innerHTML += '<option value="5">September</option>'
  28.         document.getElementById("getNextGeoPage").innerHTML += '<option value="1">August</option>'
  29.         document.getElementById("getNextGeoPage").innerHTML += '</optgroup>'
  30. }
  31.  
Jan 21 '09 #1
5 2744
acoder
16,027 Expert Mod 8TB
It's a bug in IE. Use the DOM methods, e.g. options[] with new Option(), or createElement(), setAttribute(), appendChild(), or the add() method.
Jan 22 '09 #2
Claus Mygind
571 512MB
@acoder
Ok thanks for your reply, but I am stuck again as I was when I previously posted "Dynamic Option Group" thread. A little sample code would be very helpful here.

Here is my partial attempt to load the list box with options. Corrections to this code would be very much appreciated (as is clearly visible in the code I am stuck on line 2):
Expand|Select|Wrap|Line Numbers
  1.     document.getElementById("getNextGeoPage").options[0] = new Option("Select from archive", "", true, true)
  2.     document.getElementById("getNextGeoPage").options[1] = new Option("<optgroup label="2009">'
  3.     document.getElementById("getNextGeoPage").innerHTML += '<option value="21">January</option>'
  4.     document.getElementById("getNextGeoPage").innerHTML += '</optgroup>'
  5.     document.getElementById("getNextGeoPage").innerHTML += '<optgroup label="2008">'
  6.     document.getElementById("getNextGeoPage").innerHTML += '<option value="18">December</option>'
  7.     document.getElementById("getNextGeoPage").innerHTML += '<option value="13">November</option>'
  8.     document.getElementById("getNextGeoPage").innerHTML += '<option value="7">October</option>'
  9.     document.getElementById("getNextGeoPage").innerHTML += '<option value="5">September</option>'
  10.     document.getElementById("getNextGeoPage").innerHTML += '<option value="1">August</option>'
  11.     document.getElementById("getNextGeoPage").innerHTML += '</optgroup>'
  12.  
Jan 22 '09 #3
acoder
16,027 Expert Mod 8TB
For optgroup, you'd need to create an optgroup element and set its label:
Expand|Select|Wrap|Line Numbers
  1. var el = document.createElement("optgroup");
  2. el.label = "2009";
and the options would be appended to the optgroup which in turn would be appended to the select element, e.g.
Expand|Select|Wrap|Line Numbers
  1. var opt = document.createElement('option');
  2. opt.value = "21";
  3. oOption.appendChild(document.createTextNode("January"));
  4. // append the option to the option group
  5. optGrp.appendChild(opt);
  6. // append the option group to the select
  7. sel.appendChild(optGrp);
Jan 22 '09 #4
Claus Mygind
571 512MB
Thank you very much. I was able to complete the list box with your sample code and gain some knowledge in the process. Just one aspect I am not sure about, my first option on the select box is a blank option asking the user to make a selection. While not necessary here, I would like to know how to add it anyway. The default property of "Selected = Selected"

// document.getElementById("getNextGeoPage").innerHTM L = '<option value="" selected="selected">Select from archive</option>'

Since I am creating a object "var opt" and then assigning attributes to the object "opt.value = ..."

Can I then assign the following "opt.selected = "selected"?

Expand|Select|Wrap|Line Numbers
  1.         var opt = document.createElement('option');
  2.         opt.value = "";
  3.         opt.appendChild(document.createTextNode("Select from archive"));
  4.         // append first option to the select
  5.         document.getElementById("getNextGeoPage").appendChild(opt);
  6.  
  7.         //1. create option group for 2009
  8.         var optGrp = document.createElement("optgroup");
  9.         optGrp.label = "2009";
  10.  
  11.         //2. define each options to option group for 2009 
  12.         var opt = document.createElement("option");
  13.         opt.value = "21";
  14.         opt.appendChild( document.createTextNode("January") );
  15.  
  16.         //3. append the option to the option group 2009
  17.         optGrp.appendChild(opt);
  18.  
  19.         //4. append the option group 2009 to the page selector (this is done once for each year)
  20.         document.getElementById("getNextGeoPage").appendChild(optGrp);
  21.  
  22.         //create option group 2008
  23.         var optGrp = document.createElement("optgroup");
  24.         optGrp.label = "2008";
  25.  
  26.         //December
  27.         //add each month as an option (repeat next 4 lines for each option)
  28.         var opt = document.createElement("option");
  29.         opt.value = "18";
  30.         opt.appendChild( document.createTextNode("December") );
  31.         optGrp.appendChild(opt);
  32.  
  33.         //November
  34.         var opt = document.createElement("option");
  35.         opt.value = "13";
  36.         opt.appendChild( document.createTextNode("November") );
  37.         // append the option to the option group
  38.         optGrp.appendChild(opt);
  39.  
  40.         //October
  41.         var opt = document.createElement("option");
  42.         opt.value = "7";
  43.         opt.appendChild( document.createTextNode("October") );
  44.         // append the option to the option group
  45.         optGrp.appendChild(opt);
  46.  
  47.         //September 2008
  48.         var opt = document.createElement("option");
  49.         opt.value = "5";
  50.         opt.appendChild( document.createTextNode("September") );
  51.         // append the option to the option group
  52.         optGrp.appendChild(opt);
  53.  
  54.         //August 2008
  55.         var opt = document.createElement("option");
  56.         opt.value = "1";
  57.         opt.appendChild( document.createTextNode("August") );
  58.         // append the option to the option group
  59.         optGrp.appendChild(opt);
  60.  
  61.         // append the option group to the select (this is done once for each year)
  62.         document.getElementById("getNextGeoPage").appendChild(optGrp);
  63.  
  64. //        document.getElementById("getNextGeoPage").innerHTML  = '<option value="" selected="selected">Select from archive</option>'
  65.  
Jan 23 '09 #5
acoder
16,027 Expert Mod 8TB
If you want it selected and you're using elem.selected syntax, then you need to set it to true. If you set the attribute, you can set it to "selected":
Expand|Select|Wrap|Line Numbers
  1. // set property
  2. opt.selected = true;
  3. // alternatively, set the attribute:
  4. opt.setAttribute("selected","selected");
Jan 23 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Curious George | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.