Selecting values in a selection box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • booner

    Selecting values in a selection box

    I have a form that when it loads I would like to highlight the values (from
    a DB) that have been selected in a multiple selection list (<select
    multiple="true" >.

    function onLoad()
    {
    document.forms[0].elements["multipleSelect List"].value = "<value from
    DB>";
    }

    With a single selection list I know you simply set the value appropriately
    however have not been able to find how to do this with multiple selection
    lists.

    Any help would be greatly appreciated.

    BBB


  • Michael Winter

    #2
    Re: Selecting values in a selection box

    "booner" wrote on 11/11/2003:
    [color=blue]
    > I have a form that when it loads I would like to highlight the[/color]
    values (from[color=blue]
    > a DB) that have been selected in a multiple selection list (<select
    > multiple="true" >.
    >
    > function onLoad()
    > {
    > document.forms[0].elements["multipleSelect List"].value = "<value[/color]
    from[color=blue]
    > DB>";
    > }
    >
    > With a single selection list I know you simply set the value[/color]
    appropriately[color=blue]
    > however have not been able to find how to do this with multiple[/color]
    selection[color=blue]
    > lists.
    >
    > Any help would be greatly appreciated.
    >
    > BBB[/color]

    Use the 'options' array:

    multipleSelectE lement.options[ index ].selected = boolean;

    Mike

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply)


    Comment

    • booner

      #3
      Re: Selecting values in a selection box

      That worked great.

      Any way to select the choices in the selection list based on the content in
      the listbox?

      BBB
      "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message
      news:E09sb.662$ Np.3481691@news-text.cableinet. net...[color=blue]
      > "booner" wrote on 11/11/2003:
      >[color=green]
      > > I have a form that when it loads I would like to highlight the[/color]
      > values (from[color=green]
      > > a DB) that have been selected in a multiple selection list (<select
      > > multiple="true" >.
      > >
      > > function onLoad()
      > > {
      > > document.forms[0].elements["multipleSelect List"].value = "<value[/color]
      > from[color=green]
      > > DB>";
      > > }
      > >
      > > With a single selection list I know you simply set the value[/color]
      > appropriately[color=green]
      > > however have not been able to find how to do this with multiple[/color]
      > selection[color=green]
      > > lists.
      > >
      > > Any help would be greatly appreciated.
      > >
      > > BBB[/color]
      >
      > Use the 'options' array:
      >
      > multipleSelectE lement.options[ index ].selected = boolean;
      >
      > Mike
      >
      > --
      > Michael Winter
      > M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply)
      >
      >[/color]


      Comment

      • Michael Winter

        #4
        Re: Selecting values in a selection box

        "booner" wrote on 11/11/2003:
        [color=blue]
        > That worked great.
        >
        > Any way to select the choices in the selection list based on the[/color]
        content in[color=blue]
        > the listbox?
        >
        > BBB
        > "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in[/color]
        message[color=blue]
        > news:E09sb.662$ Np.3481691@news-text.cableinet. net...[color=green]
        > > "booner" wrote on 11/11/2003:
        > >[color=darkred]
        > > > I have a form that when it loads I would like to highlight the[/color]
        > > values (from[color=darkred]
        > > > a DB) that have been selected in a multiple selection list[/color][/color][/color]
        (<select[color=blue][color=green][color=darkred]
        > > > multiple="true" >.
        > > >
        > > > function onLoad()
        > > > {
        > > > document.forms[0].elements["multipleSelect List"].value =[/color][/color][/color]
        "<value[color=blue][color=green]
        > > from[color=darkred]
        > > > DB>";
        > > > }
        > > >
        > > > With a single selection list I know you simply set the value[/color]
        > > appropriately[color=darkred]
        > > > however have not been able to find how to do this with multiple[/color]
        > > selection[color=darkred]
        > > > lists.
        > > >
        > > > Any help would be greatly appreciated.
        > > >
        > > > BBB[/color]
        > >
        > > Use the 'options' array:
        > >
        > > multipleSelectE lement.options[ index ].selected = boolean;[/color][/color]

        No reason why not. The 'options' property is just an array of Option
        objects:

        Option.text: The text displayed in the list
        Option.value: The value assigned to that list entry

        You'd probably want to use something like:

        var size = multipleSelectE lement.options. length;

        for( i = 0; i < size; ++i )
        {
        // Test the name of the list option here
        if( multipleSelectE lement.options[ i ].text == 'something' )
        {
        // Select it here
        multipleSelectE lement.options[ i ].selected = true;
        }
        }

        Mike

        --
        Michael Winter
        M.Winter@[no-spam]blueyonder.co.u k (remove [no-spam] to reply)


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Selecting values in a selection box

          Michael Winter wrote:
          [color=blue]
          > Use the 'options' array:
          >
          > multipleSelectE lement.options[ index ].selected = boolean;[/color]

          `options' is _not_ an Array (object), it is an HTMLOptionsColl ection
          object or more generally a collection. You can access the options
          *like* an array (using the index operator), but not *as* an array
          [i.e. you can't do ...options.join (''), for example].


          PointedEars

          Comment

          • Lee

            #6
            Re: Selecting values in a selection box

            Thomas 'PointedEars' Lahn said:[color=blue]
            >
            >Michael Winter wrote:
            >[color=green]
            >> Use the 'options' array:
            >>
            >> multipleSelectE lement.options[ index ].selected = boolean;[/color]
            >
            >`options' is _not_ an Array (object), it is an HTMLOptionsColl ection
            >object or more generally a collection. You can access the options
            >*like* an array (using the index operator), but not *as* an array
            >[i.e. you can't do ...options.join (''), for example].[/color]

            It is an "array".
            It is not an "Array".

            Comment

            • Michael Winter

              #7
              Re: Selecting values in a selection box

              Thomas 'PointedEars' Lahn wrote on 29 Nov 2003:
              [color=blue]
              > Michael Winter wrote:
              >[color=green]
              >> Use the 'options' array:
              >>
              >> multipleSelectE lement.options[ index ].selected = boolean;[/color]
              >
              > `options' is _not_ an Array (object), it is an
              > HTMLOptionsColl ection object or more generally a collection. You
              > can access the options *like* an array (using the index
              > operator), but not *as* an array [i.e. you can't do
              > ...options.join (''), for example].[/color]

              Yikes, when was that posted? At the time, I didn't even have a DOM
              reference, let alone use one. However, I don't believe that it really
              matters in this particular instance. In any case, you'll be pleased
              to know that I use 'collection' when describing that, and similar,
              properties now.

              Mike

              --
              Michael Winter
              M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Selecting values in a selection box

                Lee wrote:[color=blue]
                > Thomas 'PointedEars' Lahn said:[color=green]
                >> Michael Winter wrote:[color=darkred]
                >>> Use the 'options' array:
                >>>
                >>> multipleSelectE lement.options[ index ].selected = boolean;[/color]
                >>
                >> `options' is _not_ an Array (object), it is an HTMLOptionsColl ection
                >> object or more generally a collection. You can access the options
                >> *like* an array (using the index operator), but not *as* an array
                >> [i.e. you can't do ...options.join (''), for example].[/color]
                >
                > It is an "array".
                > It is not an "Array".[/color]

                Each primitive array value is converted to an
                Array object when the lookup operator (.) is
                applied to it:

                alert(typeof [1, 2, 3]); // 1|2|3
                alert([1, 2, 3].join("|")); // 1|2|3

                The constructor property of `options' is not Array but
                HTMLOptionsColl ection (or a derivative of it) and it
                lacks the most properties of the Array prototype so it
                is _not_ an /[Aa]rray( object)?/.


                PointedEars

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: Selecting values in a selection box

                  (Canceled my other posting, consider it obsolete.)

                  Lee wrote:[color=blue]
                  > Thomas 'PointedEars' Lahn said:[color=green]
                  >> Michael Winter wrote:[color=darkred]
                  >>> Use the 'options' array:
                  >>>
                  >>> multipleSelectE lement.options[ index ].selected = boolean;[/color]
                  >>
                  >> `options' is _not_ an Array (object), it is an
                  >> HTMLOptionsColl ection object or more generally a collection. You
                  >> can access the options *like* an array (using the index operator),
                  >> but not *as* an array [i.e. you can't do ...options.join (''), for
                  >> example].[/color]
                  >
                  > It is an "array".
                  > It is not an "Array".[/color]

                  According to ECMAScript(-3) there are no primitive array values in the
                  language:

                  | 4.3.2 Primitive Value
                  |
                  | A primitive value is a member of one of the types Undefined, Null,
                  | Boolean, Number, or String. A primitive value is a datum that is
                  | represented directly at the lowest level of the language
                  | implementation.

                  Thus there should not be one in ECMAScript(-3) implementations . In
                  JavaScript (1.5), there is not:

                  ,-<http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/obj.html#100845 3>
                  |
                  | Array Object
                  |
                  | JavaScript does not have an explicit array data type. However, you can
                  | use the predefined Array object and its methods to work with arrays in
                  | your applications.
                  | [...]
                  | To create an Array object:
                  |
                  | 1. arrayObjectName = new Array(element0, element1, ..., elementN)
                  | 2. arrayObjectName = new Array(arrayLeng th)
                  | [...]
                  | Array literals are also Array objects; for example, the following
                  | literal is an Array object. [...]
                  |
                  | coffees = ["French Roast", "Columbian" , "Kona"]

                  Let's test it (using Mozilla/5.0):

                  alert(new Array(1, 2, 3)); // "1,2,3" (because of
                  alert([1, 2, 3]); // "1,2,3" Array.toString( ))
                  alert(typeof new Array([1, 2, 3])); // "object"
                  alert(typeof [1, 2, 3]); // "object"
                  alert(new Array(1, 2, 3).join("|")); // "1|2|3"
                  alert([1, 2, 3].join("|")); // "1|2|3"
                  alert([1, 2, 3].constructor); /*
                  * "function Array() {
                  * [native code]
                  * }"
                  */
                  alert(new Array(1, 2, 3).constructor) ; /*
                  * "function Array() {
                  * [native code]
                  * }"
                  */

                  The constructor of object referenced by the `options' property of
                  HTMLSelectEleme nts objects is not Array but HTMLOptionsColl ection
                  (or a derivative of it), and it lacks the most properties of the
                  Array prototype, so it is _not_ an /[Aa]rray( object)?/ but a
                  collection. You can access elements of the collection with the
                  index operator `[...]', like you can with elements of an
                  /[Aa]rray( object)?/, *if* the index in the latter case is numeric
                  (there are no associative arrays in JavaScript, these are Array
                  objects having additional properties.)


                  PointedEars

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Selecting values in a selection box

                    Thomas 'Ingrid' Lahn wrote:
                    [color=blue]
                    > alert(typeof new Array([1, 2, 3])); // "object"[/color]
                    ^ ^
                    Ah ... this was a little bit too much :) It needs to be

                    alert(typeof new Array(1, 2, 3)); // "object"

                    of course. The result is correct for both variants, though,
                    the former having the array literal suprisingly converted to
                    a list, I presume.


                    PointedEars

                    Comment

                    • Richard Cornford

                      #11
                      Re: Selecting values in a selection box

                      "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                      news:3FCA1516.3 000608@PointedE ars.de...[color=blue]
                      >Lee wrote:[color=green]
                      >>Thomas 'PointedEars' Lahn said:[color=darkred]
                      >>>Michael Winter wrote:
                      >>>>Use the 'options' array:
                      >>>>
                      >>>>multipleSel ectElement.opti ons[ index ].selected = boolean;
                      >>>
                      >>>`options' is _not_ an Array (object), it is an
                      >>>HTMLOptionsC ollection object or more generally a
                      >>>collection . You can access the options *like* an array
                      >>>(using the index operator), but not *as* an array
                      >>>[i.e. you can't do ...options.join (''), for example].[/color]
                      >>
                      >>It is an "array".
                      >>It is not an "Array".[/color]
                      >
                      >Each primitive array value is converted to an
                      >Array object when the lookup operator (.) is
                      >applied to it:
                      >
                      > alert(typeof [1, 2, 3]); // 1|2|3
                      > alert([1, 2, 3].join("|")); // 1|2|3
                      >
                      >The constructor property of `options' is not Array but
                      >HTMLOptionsCol lection (or a derivative of it) and it
                      >lacks the most properties of the Array prototype so it
                      >is _not_ an /[Aa]rray( object)?/.[/color]

                      I am not sure what point you are trying to make with this. Lee appears
                      to be pointing out that if the word "Array" (initial capital) was used
                      in the context of this newsgroup then it would not be unreasonable to
                      assume that the subject was an instance of JavaScript Array. But if the
                      term used is "array" (all lowercase) then the normal English definition
                      of the word array can apply. As that definition includes:-

                      <quote cite="http://dictionary.refe rence.com/search?q=array" >
                      array
                      .. . .
                      1. An orderly, often imposing arrangement.
                      .. . .
                      </quote>

                      - it does not seem inapplicable to the options collection. Which leaves
                      Michael's original statement accurate (because the options collection is
                      an orderly arrangement of options) if a little imprecise.

                      Richard.


                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: Selecting values in a selection box

                        Richard Cornford wrote:

                        /me wrote:[color=blue][color=green]
                        > > [something canceled][/color]
                        > I am not sure what point you are trying to make with this. [...][/color]

                        None which is why I canceled it.


                        PointedEars

                        Comment

                        • Lee

                          #13
                          Re: Selecting values in a selection box

                          Thomas 'PointedEars' Lahn said:[color=blue]
                          >
                          >(Canceled my other posting, consider it obsolete.)
                          >
                          >Lee wrote:[color=green]
                          >> Thomas 'PointedEars' Lahn said:[color=darkred]
                          >>> Michael Winter wrote:
                          >>>> Use the 'options' array:
                          >>>>
                          >>>> multipleSelectE lement.options[ index ].selected = boolean;
                          >>>
                          >>> `options' is _not_ an Array (object), it is an
                          >>> HTMLOptionsColl ection object or more generally a collection. You
                          >>> can access the options *like* an array (using the index operator),
                          >>> but not *as* an array [i.e. you can't do ...options.join (''), for
                          >>> example].[/color]
                          >>
                          >> It is an "array".
                          >> It is not an "Array".[/color]
                          >
                          >According to ECMAScript(-3) there are no primitive array values in the
                          >language:
                          >
                          >| 4.3.2 Primitive Value
                          >|
                          >| A primitive value is a member of one of the types Undefined, Null,
                          >| Boolean, Number, or String. A primitive value is a datum that is
                          >| represented directly at the lowest level of the language
                          >| implementation.
                          >
                          >Thus there should not be one in ECMAScript(-3) implementations .[/color]

                          Regardless of the formal definition of the language, if a variable
                          contains multiple addressable values in an ordered sequence, it
                          is an array.

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: Selecting values in a selection box

                            Lee wrote:[color=blue]
                            > Thomas 'PointedEars' Lahn said:[color=green]
                            >> [options is not an array][/color]
                            >
                            > Regardless of the formal definition of the language, if a variable
                            > contains multiple addressable values in an ordered sequence, it
                            > is an array.[/color]

                            Bullshit. You cannot even add elements to the "array" here.


                            PointedEars

                            Comment

                            • Lee

                              #15
                              Re: Selecting values in a selection box

                              Thomas 'PointedEars' Lahn said:[color=blue]
                              >
                              >Lee wrote:[color=green]
                              >> Thomas 'PointedEars' Lahn said:[color=darkred]
                              >>> [options is not an array][/color]
                              >>
                              >> Regardless of the formal definition of the language, if a variable
                              >> contains multiple addressable values in an ordered sequence, it
                              >> is an array.[/color]
                              >
                              >Bullshit. You cannot even add elements to the "array" here.[/color]

                              Try not to get excited.

                              If a variable contains multiple addressable values in an ordered
                              sequence, it is an array. There is no requirement for arrays to
                              be writable.

                              Comment

                              Working...