how do I check if at least one of several checkboxes has been checked?

N

NotGiven

I have a form with several questions. Within each question there are
several checkboxes. I need to ensure that the user checks at least one
checkbox. They can check more but must check at least one.

How would I do this in Javascript?

Many thanks.
 
E

Erwin Moller

NotGiven said:
I have a form with several questions. Within each question there are
several checkboxes. I need to ensure that the user checks at least one
checkbox. They can check more but must check at least one.

How would I do this in Javascript?

Many thanks.

Yes, it is easy.

Name your checkboxes in your HTML.
You can check if checkbox myCheck in form myForm is checked like this:

var isChecked = document.forms.myForm.myCheck.checked;

the .checked returns true or false or errors when the object you used it on
doesn't have checked-property.

if (isChecked) {
// checked is true
alert ("checkbox myCheck is checked.");
}


Regards,
Erwin Moller
 
F

Fabian

NotGiven hu kiteb:
I have a form with several questions. Within each question there are
several checkboxes. I need to ensure that the user checks at least
one checkbox. They can check more but must check at least one.

How would I do this in Javascript?

Define a global variable. Increment/decrement it by 1 each time a
checkbox is checked/unchecked. When submitting, check to see that the
variable is >0.
 
R

Richard Cornford

"Erwin Moller"
message
var isChecked = document.forms.myForm.myCheck.checked;

the .checked returns true or false or errors when the object you
used it on doesn't have checked-property.

If the object referenced exists but has no - checked - property that
assignment should assign undefined rather than erroring. It would
produce
an error if myCheck, myForm, the forms collection and/or document did
not exist (refer to objects(/functions)).
if (isChecked) {
// checked is true
alert ("checkbox myCheck is checked.");
}

In the absence of a - checked - property, and the - isChecked - variable
having been assigned undefined the - if - statement would type-convert
undefined to boolean, yielding false.

Richard.
 
N

NotGiven

Great idea - thanks


Fabian said:
NotGiven hu kiteb:


Define a global variable. Increment/decrement it by 1 each time a
checkbox is checked/unchecked. When submitting, check to see that the
variable is >0.


--
 
T

Thomas 'PointedEars' Lahn

Fabian said:
NotGiven hu kiteb:

Define a global variable.

Evil[tm]. I would prefer to use a property of the parent "form" element
object which allows to be referred as "this.form.property" in the
"onclick" event handler of the respective "input[type='checkbox']" element.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top