Edward wrote:
> The below code builds 2 tables 4 rows by 4 cols. All cells have
> checkboxes. When checked, the checkboxes in the first column
> automatically check the remainder of the check boxes in the same row.
>
> This is working fine for tables of this size. Unfortunately, my app
> produces lots of considerably bigger tables and the 'toggle' can take
> over a minute to complete!!
>
> I assume this is because for each toggle, the all form elements are
> searched through to see if they match.
Worse than that you are re-resolving the reference to the form's -
elements - collection for each and ever property access used.
> Is it possible to change this so that a toggle only has to search the
> elements of the table it appears in - rather than the whole form?
<snip>
Yes, but as you have a consistent naming convention there is no need.
Overall, you would be hard pressed to create a worse implementation of
this script than you did. Not only is it horrendously inefficient but it
is also so evidently repetitive that it should have been obvious that
considerable simplification could be achieved by appropriate function
design.
Contrast your efforts with this version, for code size and speed:-
<html>
<head>
<title></title>
<script type="text/javascript">
function toggleCheckRow(inpt, name, startNum){
var formEls = inpt.form.elements;
var state = inpt.checked;
for(var c = (startNum+

;c >= startNum;c -= 4){
formEls[name+c].checked = state;
}
}
</script>
</head>
<body>
<form name=taskform action="check_submit.php" method=post>
<table border=1>
<tr><caption>Monday</caption></tr>
<tr><td>toggle</td><td>0</td><td>1</td><td l>2</td></tr>
<tr>
<td><input type=checkbox name="checkall_row1Monday"
onClick="toggleCheckRow(this, 'Monday', 0);"></td>
<td><input type=checkbox name=Monday0></td>
<td><input type=checkbox name=Monday4></td>
<td><input type=checkbox name=Monday8></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row2Monday"
onClick="toggleCheckRow(this, 'Monday', 1);"></td>
<td><input type=checkbox name=Monday1></td>
<td><input type=checkbox name=Monday5></td>
<td><input type=checkbox name=Monday9></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row3Monday"
onClick="toggleCheckRow(this, 'Monday', 2);"></td>
<td><input type=checkbox name=Monday2></td>
<td><input type=checkbox name=Monday6></td>
<td><input type=checkbox name=Monday10></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row4Monday"
onClick="toggleCheckRow(this, 'Monday', 3);"></td>
<td><input type=checkbox name=Monday3></td>
<td><input type=checkbox name=Monday7></td>
<td><input type=checkbox name=Monday11></td>
</tr>
</table>
<table border=1>
<tr><caption>Tuesday</caption></tr>
<tr><td>toggle</td><td>0</td><td>1</td><td l>2</td></tr>
<tr>
<td><input type=checkbox name="checkall_row1Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 0);"></td>
<td><input type=checkbox name=Tuesday0></td>
<td><input type=checkbox name=Tuesday4></td>
<td><input type=checkbox name=Tuesday8></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row2Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 1);"></td>
<td><input type=checkbox name=Tuesday1></td>
<td><input type=checkbox name=Tuesday5></td>
<td><input type=checkbox name=Tuesday9></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row3Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 2);"></td>
<td><input type=checkbox name=Tuesday2></td>
<td><input type=checkbox name=Tuesday6></td>
<td><input type=checkbox name=Tuesday10></td></tr>
<tr>
<td><input type=checkbox name="checkall_row4Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 3);"></td>
<td><input type=checkbox name=Tuesday3></td>
<td><input type=checkbox name=Tuesday7></td>
<td><input type=checkbox name=Tuesday11></td>
</tr>
</table>
</form>
</body>
</html>
Richard.