473,421 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

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["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
18 5661
"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
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
"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
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
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
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
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
(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
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
"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joe Six-Pack | last post by:
Hi, Im having problems in randomly selecting an element in an array that has not been selected before.. in other words, I have an array of answers to questions, then I want to select 5, with one...
4
by: RelaxoRy | last post by:
Is there any way to stop someone getting their mouse and selecting text on a page (without making the text an image of course). Is it possible to trap and stop click-and-drag functionality from...
7
by: Razzbar | last post by:
I have a textarea and some buttons. The buttons apply functions to the text the user selects, i.e. the good old "wrap with a tag" thing we all have seen in 100 different editors. Trouble is,...
3
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
1
by: Alvey Sidecast | last post by:
This is probably embarrassingly simple, but I've been trawling through this ng for hours now and my brain hurts. I've got an unbound multi-column listbox (multi-select=none) whose rowsource is a...
2
by: Achim Domma (Procoders) | last post by:
Hi, to allow my user to select from a list of values which are calculated on the fly, I tried to generate a list of LinkButton controls on the fly. But the connected event does not fire. ...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
1
by: reshma | last post by:
I have one password field, confirm password field, combo box in one panel. There are 2 more panels - one for individual and another for corporate. After entering password and confirm password, i...
4
by: visu | last post by:
I need a solution to my problem. the problem is I ll have a button in page ... and when i click it .. content of a div tag has to be get selected (i.e what we normally do with mouse to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.