By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

How to clear checkboxes?

P: n/a
Hi,

I have a form that is generated by an ASP page,
and I want to write a client-side function in
JavaScript to clear all the checkboxes in a form.

The problem is that the number of items on the form
depends on records in a database, and all the checkboxes
have the same name, ie:

<form name="myForm" method="post" action="MyPage.asp">
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <74**************************@posting.google.com >,
bi*********@hotmail.com (Bill S.) writes:
I have a form that is generated by an ASP page,
and I want to write a client-side function in
JavaScript to clear all the checkboxes in a form.

The problem is that the number of items on the form
depends on records in a database, and all the checkboxes
have the same name, ie:
The fact that they all have the same name actually makes it easier. See below.

<form name="myForm" method="post" action="MyPage.asp">
.
. variable number of input fields
.
<input type="checkbox" value="2" name="memberOf" checked>
<input type="checkbox" value="1" name="memberOf" checked>
.
. repeats variable number of times
.

Is there another way to do this in JavaScript?

I can have the server side create client-side variables
that would tell me the form element number for the first
checkbox and the number of checkboxes if I have to, but
I would prefer something cleaner.


function clearCheckboxes(){
for (i=0;i<document.myForm.elements.length;i++)
{
if (document.myForm.elements[i].name == 'memberOf')
{
document.myForm.elements[i].checked = false;
}
}
}

<input type="button" value="Clear Member Boxes"
onclick="clearCheckboxes()" />
--
Randy
Jul 20 '05 #2

P: n/a
Bill S. wrote:
I have a form that is generated by an ASP page,
and I want to write a client-side function in
JavaScript to clear all the checkboxes in a form.

The problem is that the number of items on the form
depends on records in a database, and all the checkboxes
have the same name, ie:
[...]

Is there another way to do this in JavaScript?


Another way? You have not even shown your way.

Anyway :-) elements of the same name create a collection.
You can iterate this collection:

var c = document.forms[...].elements["..."];
if (c)
{
for (var i = 0; i < c.length; i++)
{
c[i].checked = false;
}
}
HTH

PointedEars
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.