faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

41 of 62 people (66%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How to scroll the SELECT to make the selectedindex item visible in NN4?

Mar 6th, 2001 06:05
Peter Korsten, Enoch Ng,


Note: this will only work with single line select boxes.
To select a certain option in a select box in NN4, and to have it also 
work in IE4/5/5.5 and NN6, you need to manually set the 'selected' 
properties of all options to 'false', except the one you select.
I used similar code in a select box that was dynamically constructed, 
and then an option was selected. Alas, specifying the 'defaultSelected' 
and 'selected' parameters in the 'new Option()' call has no noticeable 
effect in NN4. This piece of code, ugly as it may be, works.
function select_option( selectbox, index )
{
    for( var i = 0; i < selectbox.options.length; i++ )
    {
        if( i == index )
            selectbox.options[i].selected = true;
        else
            selectbox.options[i].selected = false;
    }
}