Forum Moderators: open

Message Too Old, No Replies

Check/uncheck all works except in fieldset?

It works but only if you remove the fieldset element!

         

JAB Creations

10:21 am on Dec 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script works except when you have a fieldset element. What do we have to modify in the script to get it to work with the fieldset?

- 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>

Bernard Marx

1:54 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John,

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"))