Programmatically Select Option

Discussion in 'Javascript' started by J1C, Jun 16, 2005.

  1. J1C

    J1C Guest

    If I only know the value of an option is a select list, is it possible
    to programmatically select that option?

    I would be returning the value from a server-side script ...
    J1C, Jun 16, 2005
    #1
    1. Advertising

  2. J1C

    Lee Guest

    J1C said:
    >
    >If I only know the value of an option is a select list, is it possible
    >to programmatically select that option?
    >
    >I would be returning the value from a server-side script ...


    How are you returning it? If you're sending the whole page,
    just give that option tag the SELECTED attribute.
    Lee, Jun 16, 2005
    #2
    1. Advertising

  3. J1C

    Fred Oz Guest

    J1C wrote:
    > If I only know the value of an option is a select list, is it possible
    > to programmatically select that option?
    >
    > I would be returning the value from a server-side script ...
    >


    Loop through the options in the select. When you find one with the
    required value, set its selected property to true.


    function selectOpt( f, s, v ){
    var opts = document.forms[f].elements.options;
    var o, i = opts.length;
    while ( ( o == opts[--i] ) ) {
    if ( o.value == v ) {
    o.selected = true;
    return;
    }
    }
    }

    If it's a multiple select, drop the return.

    --
    Fred
    Fred Oz, Jun 16, 2005
    #3
  4. J1C

    Fred Oz Guest

    Fred Oz wrote:
    [...]
    >
    > function selectOpt( f, s, v ){
    > var opts = document.forms[f].elements.options;
    > var o, i = opts.length;
    > while ( ( o == opts[--i] ) ) {


    Agghh...

    while ( ( o = opts[--i] ) ) {
    ...

    [...]

    --
    Fred
    Fred Oz, Jun 16, 2005
    #4
  5. J1C

    J1C Guest

    thanks all!
    J1C, Jun 22, 2005
    #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. J. Hall
    Replies:
    2
    Views:
    1,038
    Jeff Thies
    Apr 21, 2004
  2. WebRaster
    Replies:
    1
    Views:
    257
    Francisco Spaeth
    Oct 26, 2005
  3. Nick Spacek

    Removing select option removes entire select

    Nick Spacek, Jun 21, 2006, in forum: Javascript
    Replies:
    1
    Views:
    140
    Matt Kruse
    Jun 21, 2006
  4. rvelosoo
    Replies:
    1
    Views:
    316
    Bart Van der Donck
    Aug 13, 2008
  5. Generale Cluster
    Replies:
    1
    Views:
    913
    Frobernik
    May 31, 2010
Loading...

Share This Page