I think my brain has already checked out and is in holiday mode. I'm trying to do something extremely simple, and I can not get it to work for the life of me.
I have a form that is dynamically generated via server-side code. It can have one or more questions that have checkboxes as options. I need to check to make sure at least one item is checked in any group, and the validation has to be done in pure JS (no jQuery).
I'm banging my head against the desk trying to get it to work:
HTML:
<form onsubmit="return validateCheckboxes();">
<h4>Which things do you enjoy?</h4>
<input type='checkbox' name='2' value='12' id='2_12'>
<label for='2_12'> Breathing</label><br />
<input type='checkbox' name='2' value='13' id='2_13'>
<label for='2_13'> Watching paint dry</label><br />
<input type='checkbox' name='2' value='14' id='2_14'>
<label for='2_14'> Nothing</label><br />
<br />
<br />
<input type="button" value="Submit">
Javascript:
function validateCheckboxes() {
if (document.querySelector('.2:checked')) {
alert('something is checked');
return true;
} else {
alert('NOTHING is checked');
return false;
}
};
jsFiddle Link: https://jsfiddle.net/r6c4hxhj/