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

Discussion in 'Javascript' started by Ferd Berfel, Apr 6, 2004.

  1. Ferd Berfel

    Ferd Berfel Guest

    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
    Ferd Berfel, Apr 6, 2004
    #1
    1. Advertising

  2. Re: <select tag> - how do I set the current index in a multiple selectbox?

    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=e...g.javascript: select box randelld&btnG=Search

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

    randelld
    Reply Via Newsgroup, Apr 6, 2004
    #2
    1. Advertising

  3. Ferd Berfel

    DU Guest

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

    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
    DU, Apr 6, 2004
    #3
  4. Ferd Berfel

    Ferd Berfel Guest

    DU <> wrote in message news:<c4tepb$ilm$>...
    > 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
    Ferd Berfel, Apr 6, 2004
    #4
  5. Ferd Berfel

    DU Guest

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

    Ferd Berfel wrote:

    > DU <> wrote in message news:<c4tepb$ilm$>...
    >
    >>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
    DU, Apr 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    779
    John C. Bollinger
    Jan 27, 2006
  2. Gnarlodious
    Replies:
    4
    Views:
    17,578
    elizas
    May 5, 2010
  3. Jim Mitchell

    How to set current page index from unique ID in dataset

    Jim Mitchell, Aug 16, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    142
    Stevie_mac
    Aug 17, 2003
  4. palmiere
    Replies:
    1
    Views:
    396
    Erwin Moller
    Feb 9, 2004
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    277
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page