First off, please note that any javascript restrictions to forms cannot be trusted. The user can always forge their own requests. Thus, always validate using PHP as well.
<fieldset>
<legend>Meds</legend>
<input class="toggleFieldset" type="checkbox" value="medicationA" checked>
<fieldset id="medicationA">
<legend>Medication Group A</legend>
<div>
<input type="radio" name="medA[]">Never
</div>
<div>
<input type="radio" name="medA[]">Sometimes
</div>
<div>
<input type="radio" name="medA[]">Always
</div>
</fieldset>
<input class="toggleFieldset" type="checkbox" value="medicationB" checked>
<fieldset id="medicationB">
<legend>Medication Group B</legend>
<div>
<input type="radio" name="medB[]">Never
</div>
<div>
<input type="radio" name="medB[]">Sometimes
</div>
<div>
<input type="radio" name="medB[]">Always
</div>
</fieldset>
</fieldset>
Handle checkbox clicks
<script>
/* Dynamically add onchange event handler to all inputs with class toggleFieldset,
instead of setting onchange="..." for each one separately.
Code runs when you click one of the checkboxes. */
$('input.toggleFieldset').bind('change', function(e) {
/* The e function parameter is the event handler */
if (e.target.checked) {
var el = $('#'+ e.target.value);
el.prop('disabled', false);
el.css('color', '#000');
}
else {
var el = $('#'+ e.target.value);
el.prop('disabled', true);
el.css('color', '#888');
}
});
Handle page reload after someone unchecks a checkbox
/* If someone unchecks a checkbox, then reload the page, without javascript you'd
have a state with unchecked checkbox but enabled fieldset, which needs to be fixed.
This is one way of doing it. Another would be to simple make sure the checkboxes are
always checked. */
$('input.toggleFieldset').each(
function() {
// $(this) will be each of the toggleFieldset inputs as jQuery iterates over them
var tf = $(this).prop('checked');
// The difference between attr and prop is that attr holds the element's attribute value from page load
// while prop holds the current value. When box is unchecked, we need attr('value')
var el = $('#'+$(this).attr('value'))
var c = tf ? '#000' : '#888';
el.prop('disabled', !tf).css('color', c);
// If you're a fan of one liners, you can reduce the above to
/*
$('#'+$(this).attr('value')).prop('disabled', !$(this).prop('checked')).css('color', ($(this).prop('checked') ? '#000' : '#888'));
*/
})
</script>