<select tag> - how do I set the current index in a multiple select box?

F

Ferd Berfel

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
 
R

Reply Via Newsgroup

Ferd said:
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


Off the top of my head:
formName.mySelect.options[5] = 1;

Your options begin from zero thus the sixth option is 5.

Search Google Groups for
comp.lang.javascript: select box randelld

or click on the link below (watch for wrapping below)

http://groups.google.ca/groups?hl=e...g.javascript:+select+box+randelld&btnG=Search

and you'll find the posts/replies that I got recently.

randelld
 
D

DU

Ferd said:
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
 
F

Ferd Berfel

DU said:
Ferd said:
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
 
D

DU

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top