Suppose I wanted you to select at least one option, but radio buttons aren't appropriate because choosing more than one option is also allowed.
<form action="" method="post">
<h1>What fruit do you enjoy eating?</h1>
<input type="checkbox" name="food[]" value="1" required/> Apples
<input type="checkbox" name="food[]" value="2" required/> Oranges
<input type="checkbox" name="food[]" value="3" required/> Watermelons
<input type="checkbox" name="food[]" value="4" required/> Tomatoes (yes they are)
<button type="submit">
Submit
</button>
</form>
What I thought would happen in this setup, was that putting the required
attribute (on all of the checkboxes) would then make the browser force you to select at least one of those checkboxes, because they share the same name
attribute. But instead, it forces you to tick all of them.
Seeing as my expected solution doesn't work, is there any other "hack" that can be employed to get the expected behaviour here, that doesn't involve javascript?