...

View Full Version : more "check all" checkboxes



hedbanger
07-10-2002, 01:08 AM
i got this code from this board here a while back and have been attempting to adapt it to my needs.

it's for an intranet and only needs to work on i.e.

selecting from a list of groups will create a list of subgroups which can be further refined by checking their boxes. there will be a checkboxes and a button for each of the dynamically created lists.

the first problem is that after the first button is clicked, the next one has to be clicked twice. once to focus it and once to click it. this is very annoying. how do i correct this?
here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>check it out</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- // Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
// End -->
</script>
</head>
<body>
<FORM ACTION="" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<!-- -->
<br>
<table width="150" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center">SET 1</td>
</tr>
<tr>
<td align="center">
<input name="button1"type="button" style="COLOR: #24559E; BACKGROUND-COLOR: #ffffff" onClick="this.value=check(this.form.elements['SRP2'])" value="Check All">
<br>
<br>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="SRP2" value="1">
A<br>
<input type="checkbox" name="SRP2" value="2">
B<br>
<input type="checkbox" name="SRP2" value="3">
C<br>
</td>
</tr>
</table>
<p>&nbsp;</p>
<p><br>
</p>
</form>
<FORM ACTION="" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<table width="150" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center">SET 2</td>
</tr>
<tr>
<td align="center">
<input name="button2"type="button" style="COLOR: #24559E; BACKGROUND-COLOR: #ffffff" onClick="this.value=check(this.form.elements['SRP2[]2'])" value="Check All">
<br>
<br>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="SRP2[]2" value="1">
&Xi; <br>
<input type="checkbox" name="SRP2[]2" value="2">
&Zeta;<br>
<input type="checkbox" name="SRP2[]2" value="3">
&Omega;<br>
</tr>
</table>
</form>
</body>
</html>

adios
07-10-2002, 01:33 AM
You've got two forms, and only one set of flags. Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>check it out</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- // Begin
function check_uncheck_all(button, name) {
var box, e = 0, grp = button.form[name];
while (box = grp[e++]) box.checked = (button.value == 'Check All');
return (button.value == 'Check All') ? 'Uncheck All' : 'Check All';
}
// End -->
</script>
</head>
<body>
<FORM ACTION="" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<!-- -->
<br>
<table width="150" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center">SET 1</td>
</tr>
<tr>
<td align="center">
<input name="button1"type="button" style="COLOR: #24559E; BACKGROUND-COLOR: #ffffff; WIDTH: 100px;" onClick="this.value=check_uncheck_all(this,'SRP2')" value="Check All">
<br>
<br>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="SRP2" value="1">
A<br>
<input type="checkbox" name="SRP2" value="2">
B<br>
<input type="checkbox" name="SRP2" value="3">
C<br>
</td>
</tr>
</table>
<p> </p>
<p><br>
</p>
</form>
<FORM ACTION="" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<table width="150" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td align="center">SET 2</td>
</tr>
<tr>
<td align="center">
<input name="button2"type="button" style="COLOR: #24559E; BACKGROUND-COLOR: #ffffff; WIDTH: 100px;" onClick="this.value=check_uncheck_all(this,'SRP2[]2')" value="Check All">
<br>
<br>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="SRP2[]2" value="1">
? <br>
<input type="checkbox" name="SRP2[]2" value="2">
?<br>
<input type="checkbox" name="SRP2[]2" value="3">
O<br>
</tr>
</table>
</form>
</body>
</html>

hedbanger
07-10-2002, 08:04 PM
excellent!
thanks.

hedbanger
10-25-2002, 10:25 PM
i've got this script loading dynamically after submitting a search.
i find that some of the results return with only 1 subclass and therefore only 1 checkbox.
the checkall/uncheckall button doesn't function for this.

it would be best to omit the button when there is only a single checkbox. how would i go about this?
thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum