Consider the following markup:
<label>
<input type="hidden" name="likes_bacon" value="no" />
<input type="checkbox" name="likes_bacon" value="yes" />
I like bacon
</label>
The W3C HTML validator will raise an error against this markup as the label contains more than one input, which is technically invalid.
My question is two fold: why should a validator care, since the first is hidden
? User agents (including screen readers - I've checked) do not make any indication that the hidden field exists, and it's quite common/useful for frameworks to render checkboxes with an accompanying hidden input with the same name in order to always pass through a default value in the form submission in cases where the form is submitted with the checkbox unchecked. The hidden input is also immutable since it is not really a user-editable form control.
Secondly, aside from the possibility of an error showing up in browser consoles and failing validation, is there any harm in doing this? I can't think of a single reason other than "you're not supposed to"!
I guess this leads on to a wider point that perhaps input type="hidden"
is a misnomer, as it is not really an input at all!
I'm well aware that it's entirely possible to move the inputs outside of the label
and reference the checkbox by id
with the for
attribute.
Loosely related: Two input fields inside one label