Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to set multiple selected options in a select-multiple type selectin scripting?

Reply
Thread Tools

How to set multiple selected options in a select-multiple type selectin scripting?

 
 
Max
Guest
Posts: n/a
 
      04-11-2008
Is there any way to set a select-multiple type <select
multiple="multiple"> with multiple selected options in scripting?
Any idea about this is appreciative.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-11-2008
Max wrote:
> Is there any way to set a select-multiple type <select
> multiple="multiple"> with multiple selected options in scripting?


Yes, objects implementing the HTMLSelectElement interface have an `options'
property that refers to an object implementing the HTMLOptionsCollection
interface. Each item of that collection is then supposedly a reference to
an object that implements the HTMLOptionElement interface, and so has a
writable `selected' property:

selectRef.options[42].selected = true;

See <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-94282980> and
<http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>.


HTH

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
 
Max
Guest
Posts: n/a
 
      04-12-2008
On 4月12日, 上午4时26分, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> Yes, objects implementing the HTMLSelectElement interface have an `options'
> property that refers to an object implementing the HTMLOptionsCollection
> interface. Each item of that collection is then supposedly a reference to
> an object that implements the HTMLOptionElement interface, and so has a
> writable `selected' property:
>
> selectRef.options[42].selected = true;
>
> See <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-94282980> and
> <http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>.
>
> HTH
>
> PointedEars
> --
> var bugRiddenCrashPronePieceOfJunk = (
> navigator.userAgent.indexOf('MSIE 5') != -1
> && navigator.userAgent.indexOf('Mac') != -1
> ) // Plone, register_function.js:16


Thanx Thomas,
It works pretty well. And I make a function like this to set
<select> option be selected with option's value:
function sel$(O,v){
if(O && O.nodeName=="SELECT"){
O.selectedIndex=-1;
if(O.type=="select-one"){
for(z=0;z<O.length;z++){
if(O.options[z].value==v)
return O.selectedIndex=z;
}
}
else if(O.type=="select-multiple"){
if(!isArray(v))return false;
for(z=0;z<O.length;z++){
if(inArray(v,O.options[z].value))
O.options[z].selected=true;
}
}
}
}
....no comments....
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-12-2008
Max wrote:
> [...] And I make a function like this to set
> <select> option be selected with option's value:
> function sel$(O,v){
> if(O && O.nodeName=="SELECT"){
> O.selectedIndex=-1;
> if(O.type=="select-one"){
> for(z=0;z<O.length;z++){
> if(O.options[z].value==v)
> return O.selectedIndex=z;
> }
> }
> else if(O.type=="select-multiple"){
> if(!isArray(v))return false;
> for(z=0;z<O.length;z++){
> if(inArray(v,O.options[z].value))
> O.options[z].selected=true;
> }
> }
> }
> }
> ....no comments....


Yes, I have some. First, you should write HTML DOM accessors
case-insensitive (use RegExp matching for that). Second, you should return
a value different from `undefined' in the "select-multiple" case. Third,
you may use switch...case instead of if..else if. Fourth, identifiers that
don't refer to constructors should not start with a capital letter to keep
the distinction.

Finally, please trim your quotes to the necessary minimum.

http://jibbering.com/faq/


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Max
Guest
Posts: n/a
 
      04-13-2008
> Yes, I have some. First, you should write HTML DOM accessors
> case-insensitive (use RegExp matching for that). Second, you should return
> a value different from `undefined' in the "select-multiple" case. Third,
> you may use switch...case instead of if..else if. Fourth, identifiers that
> don't refer to constructors should not start with a capital letter to keep
> the distinction.
>
> Finally, please trim your quotes to the necessary minimum.
>
> http://jibbering.com/faq/
>
> PointedEars
> --
> Anyone who slaps a 'this page is best viewed with Browser X' label on
> a Web page appears to be yearning for the bad old days, before the Web,
> when you had very little chance of reading a document written on another
> computer, another word processor, or another network. -- Tim Berners-Lee


My other two functions used in the function mentioned:
function isArray(a){
if(!a||typeof(a)!=='object'||!a.hasOwnProperty)ret urn false;
if(typeof(a.length)!=='number'||
a.propertyIsEnumerable('length'))return false;
return (a instanceof Array);
}
function inArray(a,v,s){
if(!isArray(a)||a.length==0)return false;
if(typeof(v)=='string'||typeof(v)=='number'){
if(s===1){
for(var z=0;z<a.length;z++){
if(a[z]===v){
return true;
}
}
}else{
for(var z=0;z<a.length;z++){
if(a[z]==v){
return true;
}
}
}
}
return false;
}
for `1st, I should re-consider whether to check the values match by
case-insensitive or case-sensitive; `2nd, if the second parameter is
not given, it should just do selectRef.selectedIndex=-1 that set the
select elem not select nothing, and for `select-multiple', the values
should be in a [Array] to be checked with; `3rd, I never consider
which is better switch ... case... and if ... else ... ; `4th, do you
mean I'd better use inarray and isarray instead of inArray and
isArray? Perhaps it is just the way I`m used to writing scripts. May
be I should avoid doing that; at last, I think I should write more
neat and strict scripts.
Thanx again for your reply.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-14-2008
Max wrote:
>> Yes, I have some. First, you should write HTML DOM accessors
>> case-insensitive (use RegExp matching for that). Second, you should return
>> a value different from `undefined' in the "select-multiple" case. Third,
>> you may use switch...case instead of if..else if. Fourth, identifiers that
>> don't refer to constructors should not start with a capital letter to keep
>> the distinction.
>>
>> Finally, please trim your quotes to the necessary minimum.


Which part of that line did you not get?

>> [...]

>
> My other two functions used in the function mentioned:
> function isArray(a){
> if(!a||typeof(a)!=='object'||!a.hasOwnProperty)ret urn false;
> if(typeof(a.length)!=='number'||
> a.propertyIsEnumerable('length'))return false;
> return (a instanceof Array);
> }


Eeek.

function isArray(a)
{
return (typeof Array != "undefined"
? a.constructor == Array
: typeof a.length != "undefined");
}

And the provision is only there for very old implementations; it can be
omitted with the optimizations below.

http://PointedEars.de/es-matrix

> function inArray(a,v,s){
> if(!isArray(a)||a.length==0)return false;
> if(typeof(v)=='string'||typeof(v)=='number'){
> if(s===1){


This line does not make sense. Why insist on a number where a
boolean-convertible value suffices?

> for(var z=0;z<a.length;z++){
> if(a[z]===v){
> return true;
> }
> }
> }else{
> for(var z=0;z<a.length;z++){
> if(a[z]==v){
> return true;
> }
> }
> }
> }
> return false;
> }


That does not strike me as being much reasonable either.

function isInIterable(v, a, bStrict)
{
// no need to try anything if not applicable or `a' is empty
if (typeof a.length != "undefined" && a.length)
{
var equals = (function() {
if (bStrict)
{
return function(x, y) {
// use eval() to hide this from very old implementations
return x === y;
};
}
else
{
return function(x, y) {
return x == y;
};
}
})();

for (var i = 0, len = a.length; i < len; i++)
{
if (equals(v, a[i]))
{
return true;
}
}
}

return false;
}

> for `1st, I should re-consider whether to check the values match by
> case-insensitive or case-sensitive;


Yes, you should: http://www.w3.org/TR/DOM-Level-2-HTM...l#ID-882764350

> `2nd, if the second parameter is not given, it should just do
> selectRef.selectedIndex=-1 that set the select elem not select nothing,
> and for `select-multiple', the values should be in a [Array] to be
> checked with;


And the return value could be an array of matching (HTML)Option(Element)
objects.

> `3rd, I never consider which is better switch ... case... and if ... else ... ;


In your case if...else may suffice. However, this method could very well
extended to return the value of an arbitrary form control, according to the
`type' property of the object representing it, where switch...case would
come in handy.

> `4th, do you mean I'd better use inarray and isarray instead of inArray
> and isArray?


Of course not. I was talking about the *DOM* (you do know what a DOM is, yes?).

> Perhaps it is just the way I`m used to writing scripts. May
> be I should avoid doing that; at last, I think I should write more
> neat and strict scripts.


Most definitely you should.

> Thanx again for your reply.


You're welcome.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
options.selected on select multiple DBoy001 Javascript 4 07-31-2006 06:09 PM
selectin viewer to download photos jww Computer Support 3 06-10-2006 07:47 PM
ListBox Programaticly Set Multiple Items as Selected Jason L Lind ASP .Net Web Controls 0 04-11-2006 10:55 PM
What is maximum amount of selected options in SELECT MULTIPLE MaJoHu ASP General 5 06-04-2004 06:38 AM



Advertisments