Basically, the reason this error occurs is because the label element should only be associated with a single input element (of any type ). Because of this, only one βform controlβ can be within the label , like only one that I can specify in the for attribute. Semantically, an input type hidden is still "form control" and therefore may have a label associated with it. The fact that this is not visible is the default CSS issue imposed by the browser, and therefore, although this may not be a good authoring practice, it is entirely possible to have
<style scoped> input[type=hidden] { display: inline-block; visibility: visible; } </style> <label> Hidden input: <input type="hidden" value="Peek-a-boo!"/> </label>
You see that the input [type = hidden] is still being entered and therefore is still a form control, and therefore you can still assign a label to it (at least according to the specifications - there may be browser errors with this) . Since this is a form control, it cannot sit next to another form control within the label . Just. As far as I know, there is nothing in the specification that says that the hidden input type cannot contain a label reference.
In the case indicated in the question, the user agent corrected the error by associating only the label with the non-hidden input , but the lesser-known UA can choose a link to either the first or the last "control form", which may not be the intention of the author.
From specification :
The LABEL element can be used to attach information to controls. Each LABEL Element is associated with one shape control.
The for attribute directly associates the label with another control: the value of the for attribute must be the same as the id value of the attribute of the associated control. More than one LABEL can be associated with the same control, creating multiple links through the for attribute.
In your specific case, you can simply move the hidden input from the label like this:
<input type="hidden" name="likes_bacon" value="no" /> <label> <input type="checkbox" name="likes_bacon" value="yes" /> I like bacon </label>
source share