469,328 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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

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
Jul 23 '05 #1
4 2676
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

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=en...ld&btnG=Search

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

randelld
Jul 23 '05 #2
DU
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
Jul 23 '05 #3
DU <dr*******@hotWIPETHISmail.com> wrote in message news:<c4**********@news.eusc.inter.net>...
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
Jul 23 '05 #4
DU
Ferd Berfel wrote:
DU <dr*******@hotWIPETHISmail.com> wrote in message news:<c4**********@news.eusc.inter.net>...
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Cristian Martinello | last post: by
7 posts views Thread by Hal Vaughan | last post: by
7 posts views Thread by Felix Natter | last post: by
6 posts views Thread by Chris Fink | last post: by
1 post views Thread by Sender | last post: by
5 posts views Thread by Isha | last post: by
7 posts views Thread by Shrek | last post: by
1 post views Thread by helraizer1 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.