Solved

Multiple attribute in Select lists

Posted on 2001-08-20
8
552 Views
Last Modified: 2013-11-19
Can anyone explain why the folling code does not work?
If you press the button once only one option is selected, pressing it twice works fine. Is this a bug in IE and if so is there a work around?

I have experimented with this to the point where if you uncomment out the alert box, it works first time but it is not practicle to have an alert box in the function.

<script language="Javascript">

function SelectAllOptions (theField)
{
     theField.multiple = true ;
     //alert("nothing") ;
     for (x=0; x<theField.options.length; x++)
     {
          theField.options[x].selected = true ;
     }
}
</script>

<form name="test">
<select name="list" size="5">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>

<input type="button" value="select all options" onclick="SelectAllOptions(this.form.list);">
</form>
0
Comment
Question by:reidca
8 Comments
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6406474
try with

<select name="list" size="5" multiple="true">

from the select tag not in the function...


hope helps
0
 

Expert Comment

by:PureSona
ID: 6406523
I changed ur code a little bit & it works fine.


<script language="Javascript">

function SelectAllOptions (theField)
{
    //theField.multiple = true ;
    //alert("nothing") ;
    for (x=0; x<theField.options.length; x++)
    {
         theField.options[x].selected = true ;
    }
}
</script>

<form name="test">
<select name="list" size="5" multiple>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>

<input type="button" value="select all options" onclick="SelectAllOptions(this.form.list);">
</form>

0
 
LVL 1

Author Comment

by:reidca
ID: 6406529
This does help but the function was designed like that deliberately. It will be used in an application where the user should not be able to select multiple options.
when the form is submitted a JavaScript routine is run that changes the type to multiple and selects all the options for submission to the server.

Thanks anyway.
0
 
LVL 1

Expert Comment

by:james_beilby
ID: 6406742
This should work. The timeout delay's set to 0, but it gives the browser a chance to update the select box multiple value.

<html>
<head>
<script language="Javascript">

function SelectAllOptions(formName,fieldName){
    theField = document.forms[formName].elements[fieldName];
    for (x=0; x<theField.options.length; x++)
         theField.options[x].selected = true;
}

function SetMultiple(listObj){
  listObj.multiple=true;
  setTimeout("SelectAllOptions('"+listObj.form.name+"','"+listObj.name+"')",0);
}
</script>
</head>
<body>
<form name="test">
<select name="list" size="5">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>

<input type="button" value="select all options" onclick="SetMultiple(this.form.list);">
</form>
</body>
</html>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:james_beilby
ID: 6406791
Just a small update - works the same as the script above except a bit tidier by passing a global object instead of the name references:

<html>
<head>
<script language="Javascript">
<!--
var listObj;

function SelectAllOptions(theField){
   theField.multiple=true;
   listObj = theField;
   setTimeout("SelectAllOptionsCont(listObj)",0);
}

function SelectAllOptionsCont(theField){
   for (x=0; x<theField.options.length; x++)
       theField.options[x].selected = true;
}
//-->
</script>
</head>
<body>
<form name="test">
<select name="list" size="5">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>

<input type="button" value="select all options" onclick="SelectAllOptions(this.form.list);">
</form>
</body>
</html>
0
 
LVL 1

Author Comment

by:reidca
ID: 6408989
Thanks very much for your help James.
The idea works fine and in the above code it works fine.

The problem I am still having is that I have multiple select lists on a form, all requiring the same feature i.e.not being a multiple select list until just before the form is submitted to the server.

I call the functions one after another i.e.
SelectAllOptions(theForm.MonitoringToolsInstalled) ;
SelectAllOptions(theForm.DiskBays) ;
SelectAllOptions(theForm.HardDiskControllers) ;
SelectAllOptions(theForm.HardDisks) ;
SelectAllOptions(theForm.MemorySlots) ;
SelectAllOptions(theForm.MemoryModules) ;
SelectAllOptions(theForm.ExpansionSlots) ;
SelectAllOptions(theForm.IPAddresses) ;
SelectAllOptions(theForm.NetworkCards) ;

What I am finding is that using the setTimeout method as above causes only the last function call to work correctly because the script is continuing to run after each call to setTimeout. So this code works fine if there is only one call to the function but not when there are many as there are at the moment. Any further thoughts on this would be very much appreciated. Thanks.
0
 
LVL 1

Accepted Solution

by:
james_beilby earned 100 total points
ID: 6409315
Going back to my first script is probably the best option. This way the global listObj is not overwritten by any following calls.

<html>
<head>
<script language="Javascript">
<!--
function SelectAllOptions(listObj){
   listObj.multiple=true;
   setTimeout("SelectAllOptionsCont('"+listObj.form.name+"','"+listObj.name+"')",0);
}

function SelectAllOptionsCont(formName,fieldName){
   theField = document.forms[formName].elements[fieldName];
   for (x=0; x<theField.options.length; x++)
        theField.options[x].selected = true;
}
//-->
</script>
</head>
<body>
<form name="test">
<select name="list" size="5">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>
<select name="list2" size="5">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>

<input type="button" value="select all options" onclick="SelectAllOptions(this.form.list);SelectAllOptions(this.form.list2);">
</form>
</body>
</html>
0
 
LVL 1

Author Comment

by:reidca
ID: 6409846
James, I'm going to accept your comment as an answer.
Basically I still have problems with this routine but it's a bit difficult to explain because you can't see the form or the server side code.

Also, the answer you have provided solved the original question so thanks for that.

In the end what I have decided to do is stick to using the multiple attribute in the HTML tag but I have written a routine in Javascript for the OnChange method that detects whether the user has selected more than one option, if so it deselects all options and warns them that this is not supported. Works quite well.

I would spend more time on this but I need to push onto other things. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now