473,379 Members | 1,355 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,379 software developers and data experts.

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

Max
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.
Apr 11 '08 #1
5 3278
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-94282980and
<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
Apr 11 '08 #2
Max
On 4ÔÂ12ÈÕ, ÉÏÎç4ʱ26·Ö, Thomas 'PointedEars' Lahn <PointedE...@web.de>
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-94282980and
<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
<selectoption 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....
Jun 27 '08 #3
Max wrote:
[...] And I make a function like this to set
<selectoption 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
Jun 27 '08 #4
Max
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.
Jun 27 '08 #5
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>
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jeff Anderson | last post by:
Is there a style for a select option's "selected" color? For example: <HTML> <BODY> <FORM NAME="form1"> <SELECT NAME="mySelect" SIZE="7" style="background-color:red;"> <OPTION>Test 1...
2
by: Targa | last post by:
I have a select box with about 5 options. I need to have one of the options, when selected, display a second select box. The second select box is populated by a database and needs to be preloaded...
2
by: Patrick | last post by:
Hello, I have a drop down list that when a value is selected the page refreshes itself but the selected value changes back to the default value. I would like to keep the selected value after the...
4
by: Mark Kolber | last post by:
I did a little searching on this but couldn't find an answer... On my website, I have a section of stories (www.midlifeflight.com/stories) There are different stores on different pages that are...
2
by: worzel | last post by:
Can't suss this out for the life of me, googling of no help either. Okay, new to win forms; I have a treeview with several root nodes, each having a single level of child nodes underneath. I also...
1
by: smash2004 | last post by:
I want to keep selected values in select list when i click on another option...with a click on a mouse... if i have 3 options in select list and i click first option..it gets selected..if i then...
2
by: Monty | last post by:
I use a SELECT dropdown as the nav interface for jumping to a chosen page number. When I setup up the SELECT element on the page, I want to show the user the current page number they are on, so, I...
6
by: rishabhshrivastava | last post by:
Hey All, Can someone suggest me a way to get the values of CheckBox(es) selected in a CheckBoxList control using JAVASCRIPT. I am pasting my current code gere but its not working need some...
6
by: artev | last post by:
if I have a select with more options, how I can know if is there an option selected; is necessary a cycle? or is there an inner property?
9
by: ajos | last post by:
Hello friends, After thinking about this for sometime, i decided to post this in the java forum. Well my problem here in detail is, i have 3 jsp pages where in a.jsp(for example) i have a combo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.