PDA

View Full Version : Clearing all checkboxes on a page


QuackHead
09-21-2002, 03:50 PM
How would I clear all the checkboxes in a page all at once (I wont know the names of each separate checkbox because they are generated dynamically on the server-side)?

Can anyone help me out?
Thanks

~Quack

Zvona
09-21-2002, 04:09 PM
<script type="text/javascript">
<!--
function resetFormElements(oForm, sType)
{
var aElems = oForm.elements;

for (var iI=0;iI<aElems.length;iI++)
{
if (aElems[iI].type = sType)
aElems[iI].checked = false;
}
}
// -->
</script>
.
.
.
<body>
<form action="">
<input type="button" value="clear checkboxes" onclick="resetFormElements(this.form,'checkbox');" />
</form>
</body>

gmorphus
09-21-2002, 04:22 PM
Hey! that's a realy nice property! thanks.

QuackHead
09-21-2002, 04:24 PM
Thanks Zvona,

worked like a charm.

I changed it around alittle - oh, and carefull cause in your if statement you forgot a double equals (the compare operator) sign.

if (aElems.type = sType)

[i]should be:

if (aElems[iI].type == sType)

I've revised the code...

<script type="text/javascript">
<!--
function resetFormElements(oForm, sType)
{
var aElems = oForm.elements;

for (var iI=0;iI<aElems.length;iI++)
{
if (aElems[iI].type == sType)
aElems[iI].checked = false;
}
}
// -->
</script>
.
.
.
<body>
<form action="">
<input type="button" value="clear checkboxes"
onclick="resetFormElements(this.form,'checkbox');" />
</form>
</body>


Thanks again

~Quack

whammy
09-21-2002, 04:48 PM
That's a commonly made error - I do it (not as much as I used to though - since I have done it so much!); and lots of developers at work do it too.

I corrected a fellow developers' work yesterday in which he had = instead of ==.

He slapped his forehead. :)

Just something for you fellow javascript lovers to keep in mind. ;)