473,473 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Check all checkbox

I'm using the following code for a checkbox that when clicked, either
checks or unchecks a group of checkboxes on a form. The code works
fine, except when there is only one checkbox in the group in which case
the check all checkbox doesn't work at all.

The code working with three checkboxes in a group is as follows, but
remove two of the three and you will see the code stops working:

<script language="javascript" type="text/javascript">
function checkAll( control, cbGroupName )
{
var cbGroup = control.form.elements[cbGroupName], i = 0, cb;
while( cb = cbGroup[i++] )
{
cb.checked = control.checked;
}
}
</script>

<form>
Check all
<input type="checkbox" onclick="checkAll( this,'select[]' );" >
<br><br>
<input type="checkbox" name="select[]" value="234">
<input type="checkbox" name="select[]" value="235">
<input type="checkbox" name="select[]" value="236">
</form>
If anyone can offer some insight into what is causing the problem I
would be very grateful.

Limbo

Jan 26 '06 #1
2 2188
li*****@surffast.com wrote:
I'm using the following code for a checkbox that when clicked, either
checks or unchecks a group of checkboxes on a form. The code works
fine, except when there is only one checkbox in the group in which case
the check all checkbox doesn't work at all.

The code working with three checkboxes in a group is as follows, but
remove two of the three and you will see the code stops working:

<script language="javascript" type="text/javascript">
Language is deprecated.

function checkAll( control, cbGroupName )
{
var cbGroup = control.form.elements[cbGroupName], i = 0, cb;
while( cb = cbGroup[i++] )
When there is only one checkbox, cbGroup is not a collection and there
is no cbGroup[i].
{
cb.checked = control.checked;
}
}
</script>

<form>
Check all
<input type="checkbox" onclick="checkAll( this,'select[]' );" >
<br><br>
<input type="checkbox" name="select[]" value="234">
<input type="checkbox" name="select[]" value="235">
<input type="checkbox" name="select[]" value="236">
</form>
If anyone can offer some insight into what is causing the problem I
would be very grateful.


Here's a modified version:

function checkAll( control, cbGroupName )
{
var cbGroup = control.form.elements[cbGroupName],
i = cbGroup.length,
cb;
if ('number' == typeof i){
while( cb = cbGroup[i++] ) {
cb.checked = control.checked;
}
} else {
cbGroup.checked = control.checked;
}
}

--
Rob
Jan 27 '06 #2
RobG wrote:
li*****@surffast.com wrote:
<script language="javascript" type="text/javascript">


Language is deprecated.


So we are using a deprecated means of communication? ;-)
SCNR

PointedEars
Feb 15 '06 #3

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

Similar topics

8
by: John Banta | last post by:
Hi, I have created a 12 month calendar where each day has a check box whereby the user can indicate if that day is available or not available for a certain event. The calendar is 'drawn' in a...
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
2
by: jimi_xyz | last post by:
Sorry if this isn't the correct group, i don't think there is a group for straight HTML. I am trying to create a type of search engine. There are two radio buttons at the top, in the middle there...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
9
by: morellik | last post by:
Dear all, I have a program that creates dinamically a web page. In the page I have the following function to check how many checkbox are checked. function tarInfo(info) { var i=0; var c=0;...
4
by: it2051229 | last post by:
Well i'm having a problem with the compatibility of javascript and PHP multiple delete check box.. i used a javascript for the "CHECK ALL BOXES" just like yahoo mail.. so my input is something like...
1
by: rjoseph | last post by:
HI Guys I have a simple check box form (using GET as the method) with a list of 10 different UK counties. I have validated the form so that users have to select no more and no less than 5 UK...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
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
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...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
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.