It is considered best-practice for label
elements in HTML to use the for
attribute to bind to an input
element:
<div>
<label for="myinput">My Input Label</label>
<input id="myinput" type="text" />
</div>
Instead of
<label>
My input Label
<input type="text" />
</label
Due to accessibility & screen readers understanding the prior example better
However, Ids must (should) be unique for the entire document in html specification Relevant
Lets assume I want to make a form render on two different parts of my website. This means I cannot use the for
attribute on the label
because it requires an id
to work correctly. How should I structure a form so that it can be duplicated in the document and still be understood by accessibility readers?
I am aware I could process the Ids on the label
/input
to guarantee they are unique. I am wondering if there is a solution that:
- Plays nice with screen readers
- Does not require
for
attribute on thelabel