5 ms·
why do i keep seeing forms where labels use the `for` attribute instead of wrapping the input in the label? relying on ids makes creating components a pain in
by mkoryak 11y ago
why do i keep seeing forms where labels use the `for` attribute instead of wrapping the input in the label?
relying on ids makes creating components a pain in the ass
- scarecrowbob 11y agoIIRC, the name attribute on the input element serves to tie it to its label as well as the id. I don't think that you need an id on the input element if you have a name.
- vbezhenar 11y agoNo, you must use id. Check it out: https://jsbin.com/yevuzejebi/edit?html,output https://jsbin.com/yevuzejebi/edit?html,output
- memco 11y agoOne reason might be for styling. Otherwise you may have to resort to wrapping the label's text in a span or something to target the label's text separately. You wouldn't use the id for styling, just to link the label and the input for accessibility.