How to insert an option into a select in IE7
<p>I have the following piece of javascript. jQuery is not available and I'm forced into IE7 mode:</p>

<pre><code>function insertOpt(idx, newopt)
{
    var l = sel.options.length;
    var ix = idx;
    if (ix &lt; 0)
        ix = l;

    for (var i=l; i&gt;ix; i--)
    {
        var opt = document.createElement('option');
        opt.value = sel.options[i-1].value;
        opt.innerHTML = sel.options[i-1].innerHTML;
        sel.options[i] = opt;
    }

    sel.options[ix] = newopt;
}
</code></pre>

<p>The first time through the select is empty, so the <code>for</code> loop does nothing and it falls over on the last line of the function <code>sel.options[ix] = newopt;</code> with the error:</p>

<pre><code>Object does not support this property or method
</code></pre>

<p>This function works in latest version of IE, but not in IE7 mode.</p>

<p>Can anyone give me an IE7 equivalent of this function please?</p>

<p>The function is passed an option which is to be inserted at the passed index.  All other options (if any) are moved up one and the new one is inserted.</p>

<p>In this initial case the value passed for idx is 0.</p>

<p><code>sel</code> is a global variable which has been populated like so:</p>

<pre><code>sel = document.getElementById("myselect");
</code></pre>