Selecting values in a selection box

B

booner

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["multipleSelectList"].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
 
M

Michael Winter

on 11/11/2003:
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["multipleSelectList"].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

Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

Mike
 
B

booner

That worked great.

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

BBB
Michael Winter said:
on 11/11/2003:
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["multipleSelectList"].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

Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

Mike
 
M

Michael Winter

on 11/11/2003:
That worked great.

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

BBB
on 11/11/2003:
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["multipleSelectList"].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

Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

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 = multipleSelectElement.options.length;

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

Mike
 
T

Thomas 'PointedEars' Lahn

Michael said:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an HTMLOptionsCollection
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
 
L

Lee

Thomas 'PointedEars' Lahn said:
Michael said:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an HTMLOptionsCollection
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].

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

Michael Winter

Thomas 'PointedEars' Lahn wrote on 29 Nov 2003:
Michael said:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an
HTMLOptionsCollection 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].

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
 
T

Thomas 'PointedEars' Lahn

Lee said:
Thomas 'PointedEars' Lahn said:
Michael said:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an HTMLOptionsCollection
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].

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

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
HTMLOptionsCollection (or a derivative of it) and it
lacks the most properties of the Array prototype so it
is _not_ an /[Aa]rray( object)?/.


PointedEars
 
T

Thomas 'PointedEars' Lahn

(Canceled my other posting, consider it obsolete.)
Thomas 'PointedEars' Lahn said:
Michael said:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an
HTMLOptionsCollection 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].

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

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.netscape.com/library/manuals/2000/javascript/1.5/guide/obj.html#1008453>
|
| 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(arrayLength)
| [...]
| 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
HTMLSelectElements objects is not Array but HTMLOptionsCollection
(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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
alert(typeof new Array([1, 2, 3])); // "object"
^ ^
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
 
R

Richard Cornford

Thomas 'PointedEars' Lahn said:
Lee said:
Thomas 'PointedEars' Lahn said:
Michael Winter wrote:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an
HTMLOptionsCollection 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].

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

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
HTMLOptionsCollection (or a derivative of it) and it
lacks the most properties of the Array prototype so it
is _not_ an /[Aa]rray( object)?/.

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.reference.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.
 
L

Lee

Thomas 'PointedEars' Lahn said:
(Canceled my other posting, consider it obsolete.)
Thomas 'PointedEars' Lahn said:
Michael Winter wrote:
Use the 'options' array:

multipleSelectElement.options[ index ].selected = boolean;

`options' is _not_ an Array (object), it is an
HTMLOptionsCollection 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].

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

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.

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

Thomas 'PointedEars' Lahn

Lee said:
Thomas 'PointedEars' Lahn said:
[options is not an array]

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

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


PointedEars
 
L

Lee

Thomas 'PointedEars' Lahn said:
Thomas 'PointedEars' Lahn said:
[options is not an array]

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

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

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.
 
L

Lasse Reichstein Nielsen

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

That is one defintion of "array", using the common definition of the
word, not the technical one.

If anybody talks abouts "arrays" in a discussion about a typed
language like Java, I would be surpriced if they meant something
except the language's array type.

Javascript is a less strict language. The array subscript notation can
be used on any object, as can the methods of the prototype, so the
only thing that makes arrays special are their magic length property.

So, I can see why some people would use the word "array" about objects
that only resemble an array. They should be aware that it is
potentially confuzing. When I read "array", I understand it as meaning
"instanceof Array", unless it is clear from the context that it's not.
(I.e., I agree with PointedEars, except for the way of saying it.)
/L
 
L

Lee

Lasse Reichstein Nielsen said:
That is one defintion of "array", using the common definition of the
word, not the technical one.

If anybody talks abouts "arrays" in a discussion about a typed
language like Java, I would be surpriced if they meant something
except the language's array type.

In my original post to this thread, I very clearly differentiated
between an "Array", and an "array".
 
L

Lasse Reichstein Nielsen

Lee said:
In my original post to this thread, I very clearly differentiated
between an "Array", and an "array".

You did. The original answer (by Michael Winter, IIRC) that started
this the thread on this subject, did not.

You said it was an "array", not an "Array" (without explaining what
the difference is). While that is sufficient to make me understand
what you mean, I actually don't like using "an Array" about instances
created from the "Array" constructor ... if anything, I'd prefer "and
instance of Array". Chalk it up to taste :)

Had it been a class based language, I wouldn't have any problem
calling an instance of a class by the class name, e.g. "an Apple" for
an instance of the class "Apple". It's just that Javascript doesn't
have classes. :)

/L
 
L

Lee

Lasse Reichstein Nielsen said:
You did. The original answer (by Michael Winter, IIRC) that started
this the thread on this subject, did not.

You said it was an "array", not an "Array" (without explaining what
the difference is). While that is sufficient to make me understand
what you mean, I actually don't like using "an Array" about instances
created from the "Array" constructor ... if anything, I'd prefer "an
instance of Array". Chalk it up to taste :)

Had it been a class based language, I wouldn't have any problem
calling an instance of a class by the class name, e.g. "an Apple" for
an instance of the class "Apple". It's just that Javascript doesn't
have classes. :)

It was an informal use, intended to make the point concisely.

Going back to the original point, but using more precise terminology,
it seems as if you agree with me that the options attribute of the
Select object is an array, albeit not an instance of an Array.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top