473,659 Members | 2,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9569
_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.element s;
var x, i = els.length;
while ( i-- ) {
x = els[i];
if ( 'input' == x.nodeName.toLo werCase() &&
'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.toLo werCase() &&

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.toLo werCase() &&
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
21163
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 value="cat">Cat <option value="dog">Dog ....
4
8414
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 work. <script language="JavaScript"> function selectAllCheckBox() { //alert(document.addzone.c1.value); document.addzone.c1.value = "on"; } </script>
4
6592
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 one checkbox (these are dynamically generated). Why does this fail? If I do an alert(field_to_check.length) with 1 element in the array like this I get 'Undefined'. Is this because this datatype is not an array if there is only 1 element here? ...
0
1322
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. I've done it in SelectedIndexChanged event. My code is below. However, it takes too long to work on the server side. I'm sure it is possible to do the same in Javascript. Unfortunatley, I don't know it that
2
12820
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 Select list. This is what I tried. Any help is appreciated. ============== <script type="text/javascript"> function GetOptVal(OptVal)
0
3026
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 Checkbox Header Column 2-9: BoundField Column 10: img field If the user select one checkbox item, it will open up another gridview (multiple selection does nothing). If select a row item, it will open a different aspx page. If select...
21
29771
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 prodcut anme have used ajax to avoid refreshing of page this works fine 2.now i have created one row with checkbox|select box|text|text|text|text| where in the select box values are fetched from table here also i have used ajax for getting the m_name...
3
2992
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 "Select ALL" checkbox that selects or unselects all checkboxes in the fieldset. My problem is that when I select the "Select ALL" checkbox, it selects every checkbox in the form, not just those in the fieldset. Here is the code for the "Select...
7
2166
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 category if checked with my checkbox ScreenShot: http://anthelo.org/errorSele.png Here is my code:
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.