[thelist] Internet explorer selection list counting bug?

张韡武 zhangweiwu at realss.com
Sat Sep 16 02:18:58 CDT 2006

Hello. I encounted this problem when I work for a website that require a
<select> list being generated on-the-fly with javascript. I find it very
difficult to set an option pre-selected by setting 'selected' of the
option element.

e.g. the following HTML source makes entry 'c' pre-selected on Firefox
(expected), and make entry 'b' pre-selected on IE (surprise).
(see http://weiwu.freeshell.org/ie_selection_counting_bug.html )

How do I write javascript to pre-select dynamically-generated-options
that work for both IE and Firefox?

<select id="sel">
<script type="text/javascript">
        var opts = ['a','b','c','d'];
        var sel = document.getElementById("sel");
        for(var i=0; i<opts.length; i++) {
                //var opt = new Option(opts[i],i,false,i==2?true:false);
                var opt = document.createElement("option");
                opt.text = opts[i];
                opt.value = i;
                        opt.selected = true;
                sel.options[sel.options.length] = opt;

More information about the thelist mailing list