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

how to select all checkbox?

I have n checkboxes and 1 checkbox 'SELECT ALL'.
for example:
<form action="" method="get">
<input name="sa" type="checkbox" value="v"> select all
<input name="c1" type="checkbox" value="v"> option 1
<input name="c2" type="checkbox" value="v"> option 2
<input name="c3" type="checkbox" value="v"> option 3
....
<input name="cn" type="checkbox" value="v"> option n
</form>
I'd like check all checkboxes when I select 'sa' (select all) and deselect
all boxes when I select one checkbox (less the checkbox I had selected)
How can I do that?
Thank you in advance,
Andrea.
Jul 31 '05 #1
5 9549
_andrea.l wrote:
I have n checkboxes and 1 checkbox 'SELECT ALL'.
for example:
<form action="" method="get">
<input name="sa" type="checkbox" value="v"> select all
<input name="c1" type="checkbox" value="v"> option 1
<input name="c2" type="checkbox" value="v"> option 2
<input name="c3" type="checkbox" value="v"> option 3
...
<input name="cn" type="checkbox" value="v"> option n
</form>
I'd like check all checkboxes when I select 'sa' (select all) and deselect
all boxes when I select one checkbox (less the checkbox I had selected)
How can I do that?


<script type="text/javascript">
function checkAll( el, tick ) {
var els = el.form.elements;
var x, i = els.length;
while ( i-- ) {
x = els[i];
if ( 'input' == x.nodeName.toLowerCase() &&
'checkbox' == x.type ) {
x.checked = tick;
}
}
}

function checkThis( el ) {
checkAll( el, false );
el.checked = true;
}

</script>

<form action="">
<input name="sa" type="checkbox" value="v" onclick="
checkAll(this, true);
"> select all<br>
<input name="c1" type="checkbox" value="v" onclick="
checkThis(this);
"> option 1<br>
<input name="c2" type="checkbox" value="v" onclick="
checkThis(this);
"> option 2<br>
<input name="c3" type="checkbox" value="v" onclick="
checkThis(this);
"> option 3<br>
<input name="cn" type="checkbox" value="v" onclick="
checkThis(this);
"> option n<br>
<input type="reset">
</form>

--
Rob
Jul 31 '05 #2
_andrea.l wrote:
I'd like check all checkboxes when I select 'sa' (select all) and
deselect all boxes when I select one checkbox (less the checkbox I
had selected) How can I do that?


I have a lib for this purpose, if that would help. See code and examples at:
http://www.javascripttoolbox.com/checkboxgroup/

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 31 '05 #3
I get the opportunity to thank you and all the people how had answer or just
read all my questions (this and all in the past).

I have a question:
I don't understand well this line:
if ( 'input' == x.nodeName.toLowerCase() &&

what is userfull for?

Thank you in advance,
Andrea.
Aug 1 '05 #4
_andrea.l wrote:
I get the opportunity to thank you and all the people how had answer or just
read all my questions (this and all in the past).

I have a question:
I don't understand well this line:
if ( 'input' == x.nodeName.toLowerCase() &&
what is userfull for?


That test is within a while loop, it looks to see if the current node is
an input element and if it is, it then checks to see if it's a checkbox.

If either of the above are not true, the loop goes to the next element.

If both the above tests are true, the checkbox is set to checked or not
depending on whether the 'tick' parameter is true or false respectively.

It is important to do a sequence of tests as simply assuming that the
node is a checkbox and attempting set its 'checked' property may cause
an error. By testing in sequence with an AND ( && ) test, the 'if'
statement should pass or fail gracefully for any element it may come across.

I should note that the above is a bit of 'belt and braces' approach.
You should be able to set a 'checked' attribute on any form element
(even those that have no 'checked' attribute defined in the HTML
specification) without generating an error, but I can't guarantee that
will occur in every case for all browsers.

Thank you in advance,
Andrea.

--
Rob
Aug 1 '05 #5
RobG wrote:
[...]
I should note that the above is a bit of 'belt and braces' approach. You
should be able to set a 'checked' attribute on any form element (even


I'll correct that to ' any form control or HTML element '.

[...]

--
Rob
Aug 1 '05 #6

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

Similar topics

2
by: RC | last post by:
Order for PHP get the array from a HTML form checkbox or select tag. The HTML name MUST with . For example: <form methos=POST action="myfile.php"> <select name="pets" multiple> <option...
4
by: Matt | last post by:
In ASP page, there is a "SELECT ALL" button, when user click it, it will select all checkboxes. I am not sure should I use client-side code to do that? the following is my approach but it didnt...
4
by: Michael Champagne | last post by:
We have an application to where you can select/deselect all checkboxes in a checkbox array by clicking a 'master' checkbox at the top of the screen. This seems to work fine unless there is only...
0
by: Peter Afonin | last post by:
Hello, I have 5 checkboxes in one record in the datagrid. When the user checks 5th checkbox (or, in my case, clicks the button - doesn't matter to me), all other checkboxes are checked as well....
2
by: bigrich | last post by:
I'm a beginner to javascript and need help. I've searched the forum but can't piece the answer together. I need to be able to uncheck and disable a checkbox based on the option selected in a...
0
by: New2ASP | last post by:
Thanks everyone in advance for your help. I am fairly new to web development but an experienced window-based developer. Here's the structure of my Gridview Column 1 : Checkbox with SelectAll...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
3
by: jmartmem | last post by:
Greetings, I have an ASP page containing a Record Insertion Form with a number of fields linked to an Access database. Within this form, I have a fieldset with six checkboxes, one of which is a...
7
by: avraamG13 | last post by:
Hello, I'm having a code that shows in a list checkbox and next the row name i have on my database I want to select a checkbox and when i press display to show me tha information of the specific...
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.