Using jquery, given multiple checkboxes, how please would we toggle a fieldset on/off if a particular checkbox is checked/ unchecked? (more than one checkbox may be chosen as this is multiple)
I've tried using ':checked' and 'jQuery.inArray' together with toggle() but no luck yet. Example HTML without the script:
<form>
Which food group do you like?
<input type="checkbox" name="nutrition[]" value="Fruit">
<input type="checkbox" name="nutrition[]" value="Vegetables">
<input type="checkbox" name="nutrition[]" value="Sweets">
<!-- show this input (toggle on/off) only if 'Fruit' is one of the checked boxes-->
<fieldset id="someid" style="display:none;>
You chose Fruit! Name one fruit: <input type="text" name= "afruit" />
</fieldset>
</form>
?
here is the complete html + script based on Jason P's answer, using id as selector:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
window.onload = function() {
$('#fruitid').change(function(e) {
$('#someid').toggle(this.checked);
});
};
</script>
</head>
<body>
<form>
Which food group do you like?
<input type="checkbox" name="nutrition[]" value="Fruit" id="fruitid">
<input type="checkbox" name="nutrition[]" value="Vegetables">
<input type="checkbox" name="nutrition[]" value="Sweets">
<!-- show this input (toggle on/off) only if 'Fruit' is one of the checked boxes-->
<fieldset id="someid" style="display:none;">
You chose Fruit! Name one fruit: <input type="text" name= "afruit" />
</fieldset>
</form>
</body>
</html>