473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to clear checkboxes?

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
2 18617
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
1
by: bestafor | last post by:
I have created, in Borland c++ builder 5.0, a dialog box With 24 CheckBoxes, named CheckBox1 through CheckBox24. My question is how may I clear all Twenty-four CheckBoxes by pressing a “Clear”...
5
by: Matt | last post by:
I want to write a generic clearForm function to clear the form when the user click CLEAR button. Here's my attempts, because I want to take care all html controls. I think I need to test if the...
2
by: Tina | last post by:
Is there anyway to clear a checkbox list (uncheck all items) on the client side without causing a trip to the server? If so, could someone show me how? Thanks, T
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
3
by: Robert Kilroy | last post by:
Greetings, I've been working on this for a few hours now. It seems to be a pretty simple task but I keep running into " has no properties". I have a select box defined as follows: <SELECT...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
2
by: mcmahon | last post by:
Hi, I have a pile of checkboxes on one particular form and would like to clear them all at the form load rather that saying chk1.Checked = False for all of them. Here is the code I was trying...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.