473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

<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 2856
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cristian Martinello | last post by:
How can I add <OPTION> to a tag <SELECT> runtime ? the code combo.innerHTML+="<OPTION value='1'>Name</OPTION>" doesn't work... -- Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
7
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
7
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option...
2
by: Manfred | last post by:
Hi I try to change the border of the <select> Tag to a thin, black line. On IE no reaction! <select style="border:1px solid #000000;background-color:red;"> <option>hallo</option>...
6
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
1
by: Sender | last post by:
I tried putting a title="description" tag in an <option> tag, but when I hover the mouse over the option of the <select> tag in my browsers, no description shows up. How can I do it? Also, my...
5
by: Isha | last post by:
Hi all, I wanted to change the background color for only the first option in a select box, but following changed the background color for the whole dropdown box. <select name="alltags"...
7
by: Shrek | last post by:
I have a drop down on a web page and want to change the cursor from default to pointer, so my style definition has style ="cursor: pointer;" the drop down though fails to change from the...
1
by: helraizer1 | last post by:
Hi all, On my current project MyChatbox I have a box for which the user enters a name and the value of which is: Name: <input type="textbox" name="username" onkeyup="countdown(username, 10,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.