Ferd said:
DU said:
Ferd Berfel wrote:
given this code:
<select name="mySelect" size="3" multiple>
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
<option selected>six</option>
<option selected>seven </option>
</select>
how can I make the option "six" visible programatically?
tia
document.forms["FormName"].mySelect.selectedIndex = 5;
DU
Thank you for your response - the only problem is that I am populating
the <select> from a database - I won't know which one is "selected" -
I guess I was hoping there was a Scroll option, or something that
would allow the selected option, regardless of where it was, to come
to the top of the list. sounds like there isn't an easy option.
thanx again.
ferd
Quite on the contrary. Let's say your first selected item (among several
others which maybe selected as well; your select is multiple) is item
"n". Then you can loop through each option to see if their selected
attribute is set and if so, then assign the loop iterator. That's 1
loop, 1 if statement and 2 instructions. E.g.:
for(var intLoopOptionIterator = 0; intLoopOptionIterator <
document.forms["FormName"].mySelect.length; intLoopOptionIterator++)
{
if(document.forms["FormName"].mySelect.options[intLoopOptionIterator].selected)
{
document.forms["FormName"].mySelect.selectedIndex = intLoopOptionIterator;
break;
};
};
If you want to programmatically have the last selected option (among a
subset of all options) to be shown in the select, then just remove the
break instruction.
DU