By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

Selecting values in a selection box

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"booner" wrote 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

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #2

P: n/a
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.uk> wrote in message
news:E0******************@news-text.cableinet.net...
"booner" wrote 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

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

Jul 20 '05 #3

P: n/a
"booner" wrote 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
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:E0******************@news-text.cableinet.net...
"booner" wrote 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

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #4

P: n/a
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].
PointedEars
Jul 20 '05 #5

P: n/a
Lee
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".

Jul 20 '05 #6

P: n/a
Thomas 'PointedEars' Lahn wrote on 29 Nov 2003:
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].


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.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #7

P: n/a
Lee wrote:
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)?/.
PointedEars
Jul 20 '05 #8

P: n/a
(Canceled my other posting, consider it obsolete.)

Lee wrote:
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. 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
Jul 20 '05 #9

P: n/a
Thomas 'Ingrid' Lahn wrote:
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
Jul 20 '05 #10

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Lee wrote:
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.
Jul 20 '05 #11

P: n/a
Richard Cornford wrote:

/me wrote:
[something canceled]

I am not sure what point you are trying to make with this. [...]


None which is why I canceled it.
PointedEars
Jul 20 '05 #12

P: n/a
Lee
Thomas 'PointedEars' Lahn said:

(Canceled my other posting, consider it obsolete.)

Lee wrote:
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.

Jul 20 '05 #13

P: n/a
Lee wrote:
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
Jul 20 '05 #14

P: n/a
Lee
Thomas 'PointedEars' Lahn said:

Lee wrote:
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.

Jul 20 '05 #15

P: n/a
Lee <RE**************@cox.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com DHTML Death Colors:
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without
judgement merely degrades the spirit divine.'
Jul 20 '05 #16

P: n/a
Lee
Lasse Reichstein Nielsen said:

Lee <RE**************@cox.net> writes:
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.


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

Jul 20 '05 #17

P: n/a
Lee <RE**************@cox.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #18

P: n/a
Lee
Lasse Reichstein Nielsen said:

Lee <RE**************@cox.net> writes:
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 "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.

Jul 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.