I'd like to figure out how to write something like the following to validate single check boxes. There could be just one on the form or many separate ones. The example below doesn't work.
Thank you!
// -----------------------------------------------
// CHECK SINGLE CHECKBOX
// -----------------------------------------------
$('.mcCbxRequired').each(function() {
var mcCbxCheck = $(this);
if(mcCbxCheck.is(':checked')) {
alert('checked');
// do something here ...
}
else{
alert('not checked');
return false;
}
});
A couple things:
Each applicable checkbox must have the mcCbxRequired
class. If neither alert shows, the problem must be because your checkbox does not have this class.
You are return
ing false
in both cases. That doesn't really make sense with validation, so you should change the relevant part of your code to this:
Code:
if(mcCbxCheck.is(':checked')) {
alert('checked');
return true;
}
You can use:
$('input[type=checkbox]:checked').each(function()
{
var checkedBox = $(this);
// Do whatever you want
});
Also, JQuery selectors by Class are slower than by id/by type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With