Forum Moderators: open
- John
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Check Boxes</title><script type="text/javascript">
function Checkall(form){
for (var i = 1; i < form.elements.length; i++){
eval("form.elements[" + i + "].checked = form.elements[0].checked");
}
}
</script>
</head><body>
<form>
<fieldset>
<table>
<colgroup style="width: 20px;"></colgroup>
<colgroup width="33%"></colgroup>
<colgroup width="33%"></colgroup>
<colgroup width="33%"></colgroup>
<thead>
<tr>
<td><input onclick="Checkall(this.form);" type="checkbox" value="" /></td>
<td>From
</td>
<td>Subject
</td>
<td>Recieved
</td>
</tr>
</thead>
<tfoot><tr>
<td><input type="checkbox" value="" />
</td>
<td>From
</td>
<td>Subject
</td>
<td>Recieved
</td>
</tr></tfoot>
<tbody>
<tr>
<td><input type="checkbox" value="" />
</td>
<td>From
</td>
<td>Subject
</td>
<td>Recieved
</td></tr>
<tr>
<td><input type="checkbox" value="" />
</td>
<td>From
</td>
<td>Subject
</td>
<td>Recieved
</td></tr>
<tr>
<td><input type="checkbox" value="" />
</td>
<td>From
</td>
<td>Subject
</td>
<td>Recieved
</td></tr>
</tbody>
</table>
</fieldset>
</form></body>
</html>
The problem is that a FIELDSET is considered a form element (ie. member of a form's elements collection). This means that the index of the reference checkbox is actually 1 in this case, since the element 0 is the fieldset.
(The code is also adding an expando checked property to any fieldset element it inspects, but that doesn't really matter).
Using a DOM method to collect the inputs will release you from this problem (eg. doc.getElementsByTagName("input"))