Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > <select tag> - how do I set the current index in a multiple select box?

Reply
Thread Tools

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

 
 
Ferd Berfel
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
 
 
 
Reply Via Newsgroup
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
 
 
 
DU
Guest
Posts: n/a
 
      04-06-2004
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
 
Reply With Quote
 
Ferd Berfel
Guest
Posts: n/a
 
      04-06-2004
DU <(E-Mail Removed)> wrote in message news:<c4tepb$ilm$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
DU
Guest
Posts: n/a
 
      04-06-2004
Ferd Berfel wrote:

> DU <(E-Mail Removed)> wrote in message news:<c4tepb$ilm$(E-Mail Removed)>...
>
>>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to set multiple selected options in a select-multiple type selectin scripting? Max Javascript 5 04-14-2008 01:02 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM
How to set current page index from unique ID in dataset Jim Mitchell ASP .Net Datagrid Control 1 08-17-2003 12:20 AM



Advertisments