Adding option elements to select on focus

Discussion in 'Javascript' started by Josh C., Mar 14, 2008.

  1. Josh C.

    Josh C. Guest

    I am attempting to dynamically add an option to a select box when the
    box is selected (click or focus). This works fine using Firefox by
    either creating a new element and appending it to the SELECT's
    children, or by creating a new Option() and setting it to the
    select.options array.

    My problem is in IE whenever it adds the element the select box does
    not drop down. For instance if I have my code run onFocus when the
    select box is clicked the option is added and the box does not drop
    down. If I click it a second time while it still has focus it will
    display the new value.

    Is there a trick or work around to have IE handle this the same as FF?

    thanks,
    jc
     
    Josh C., Mar 14, 2008
    #1
    1. Advertisements

  2. Josh C.

    SAM Guest

    Josh C. a écrit :
    IE doesn't leave focus so easily

    add : this.options.selectedIndex = this.options.length-1;

    try also : onmousedown (... ? ! ! ? )

    <script type="text/javascript">
    function extend(quoi) {
    quoi = quoi.elements['ici'];
    quoi.length=0;
    for(var i=0; i<20; i++)
    quoi.options[quoi.length] = new Option(i, i);
    quoi.options.selectedIndex=quoi.length-1;
    }
    </script>
    <form action="#" onsubmit="return confirm('sure?');">
    <p><select name='ici' onfocus="extend(this.form);">
    <option>test</option>
    </select>
    </form>

    My browsers (IE Mac Fx ...) works all the same : while you don't click
    outside the select, focus stay on it and the function do not more fire.
     
    SAM, Mar 14, 2008
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.