Is a for attribute mandatory?

Do you ABSOLUTELY need a for attribute?

No, not in all cases. Only when the label element is a sibling of the input control is a for attribute recommended. From an assistive technology perspective it is a required attribute under accessibility guidelines (need to read up on this).

The LABEL is optional, when it comes down to it. It does make a lot of sense, though.

Here is a typical form from olden times…

<form>
<p>text <input></p>
<p><input> text</p>
</form>

But some authors used DIV, some used LI, and it was kind of up in the air in HTML 3.

HTML 4.0 introduced the LABEL element. Give this page a read,

https://www.w3.org/TR/REC-html40-971218/interact/forms.html#h-17.9.1

and go to Label on MDN to read the documentation in modern terms.


Aside

When the INPUT is a child of LABEL, id and for are not needed, and rather moot. In reading accessibility guidelines, look for this distinction. It may be required (though not needed) in order to validate under WAI guidelines.

2 Likes