By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

Check all checkbox

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.