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

Select from a list

Is there a way to select all the elements of a list via a JavaScript?

This is a way it is

<form action="" method="post" enctype="application/x-www-form-urlencoded">
<select name="select" size="6" multiple>
<option value="TEST1">TEST1</option>
<option value="TEST2">TEST2</option>
<option value="TEST3">TEST3</option>
</select>
<P>
<input type="button" name="Button" value="Select All">
</form>

I would like to end up after clicking on the button to be like this

<form action="" method="post" enctype="application/x-www-form-urlencoded">
<select name="select" size="6" multiple>
<option value="TEST1" selected>TEST1</option>
<option value="TEST2" selected>TEST2</option>
<option value="TEST3" selected>TEST3</option>
</select>
<P>
<input type="button" name="Button" value="Select All">
</form>

Is there any way to do this.

Thanks for any help you can give!!!

Rick
Jul 23 '05 #1
3 1491
Rick wrote:
Is there a way to select all the elements of a list via a JavaScript?

This is a way it is

<form action="" method="post" enctype="application/x-www-form-urlencoded">
<select name="select" size="6" multiple>
<option value="TEST1">TEST1</option>
<option value="TEST2">TEST2</option>
<option value="TEST3">TEST3</option>
</select>
<P>
<input type="button" name="Button" value="Select All">
</form>

I would like to end up after clicking on the button to be like this

<form action="" method="post" enctype="application/x-www-form-urlencoded">
<select name="select" size="6" multiple>
<option value="TEST1" selected>TEST1</option>
<option value="TEST2" selected>TEST2</option>
<option value="TEST3" selected>TEST3</option>
</select>
<P>
<input type="button" name="Button" value="Select All">
</form>

Is there any way to do this.


Here's your button (presumes the select has at least one option):

<input type="button" name="Button" value="Select All" onclick="
var o = this.form.elements['select'].options;
var i = 0;
do {
o[i].selected = true;
} while ( o[++i] )
">

--
Rob
Jul 23 '05 #2
"RobG" <rg***@iinet.net.auau> wrote in message
news:nG*****************@news.optus.net.au...
Rick wrote:
Is there a way to select all the elements of a list via a JavaScript?

This is a way it is

<form action="" method="post"
enctype="application/x-www-form-urlencoded">
<select name="select" size="6" multiple>
<option value="TEST1">TEST1</option>
<option value="TEST2">TEST2</option>
<option value="TEST3">TEST3</option>
</select>
<P>
<input type="button" name="Button" value="Select All">
</form>

I would like to end up after clicking on the button to be like this

<form action="" method="post"
enctype="application/x-www-form-urlencoded">
<select name="select" size="6" multiple>
<option value="TEST1" selected>TEST1</option>
<option value="TEST2" selected>TEST2</option>
<option value="TEST3" selected>TEST3</option>
</select>
<P>
<input type="button" name="Button" value="Select All">
</form>

Is there any way to do this.


Here's your button (presumes the select has at least one option):

<input type="button" name="Button" value="Select All" onclick="
var o = this.form.elements['select'].options;
var i = 0;
do {
o[i].selected = true;
} while ( o[++i] )
">


WOW this worked sooo good. Thanks very much!!!

Is there anyway to make sure the put at least one entry in the list?

Thanks again!!!
Jul 23 '05 #3
Rick wrote:
"RobG" <rg***@iinet.net.auau> wrote in message
news:nG*****************@news.optus.net.au...

[...]

Here's your button (presumes the select has at least one option):

<input type="button" name="Button" value="Select All" onclick="
var o = this.form.elements['select'].options;
var i = 0;
do {
o[i].selected = true;
} while ( o[++i] )
">

WOW this worked sooo good. Thanks very much!!!

Is there anyway to make sure the put at least one entry in the list?


To make it more robust, put the following in a script:

function checkAll(el){
if ( el && /select/i.test(el.nodeName) ){
var o = el.options;
var i = o.length;
while ( i-- ) { o[i].selected = true; }
}
}

And call it with:

<input type="button" name="Button" value="Select All"
onclick="checkAll(this.form.elements['select'])">

Now you don't care if the element doesn't exist or has no options, no
error messages will pop up. If it's not a multiple select, the first
option will end up being selected.

--
Rob
Jul 23 '05 #4

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

Similar topics

0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
3
by: Stewart | last post by:
Dear comp.lang.javascript, I have more than once wanted to manipulate the contents of select boxes dynamically, whilst the boxes contain <optgroup> tags. Manipulation of a select box containing...
5
by: callmebill | last post by:
I'm relatively new to javascript, and I'm trying to decide whether the following (and if so, clues on how to do it): I'd like to create two HTML multiple-select boxes. The first would be a list...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
4
by: Ian Richardson | last post by:
Hi, The function I've put together below is a rough idea to extend a SELECT list, starting from: <body> <form name="bambam"> <select id="fred"> <option value="1">1</option> <option...
7
chunk1978
by: chunk1978 | last post by:
hello. so i have 2 select menus which add and remove options from a 3rd select menu... it seems, however, that it's not possible to use different select menus to toggle a 3rd, because when an...
16
by: Richard Maher | last post by:
Hi, I have this Applet-hosted Socket connection to my server and in an ONevent/function I am retrieving all these lovely rows from the server and inserting them into the Select-List. (The on...
4
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.