Do we have to use for attribute in the label?

Hi,

As for definition goes :
The for attribute specifies which form element a label is bound to.

I am not sure if we have to use for in the label to bound the input. What are the disadvantages of not using for?

This depends. The reason you assign a for and id to the label and input respectively is so that there is a relationship between them.

For instance if I were scanning your form with a screen reader and you had no relationship between a label and the corresponding input, then it would be very difficult for me to know what data the input is requesting from me.
The relationship between the elements also allows a user to click the label and have focus on the corresponding input.

With that said, nesting the input inside of the label would create the same relationship, and it would be fine to omit the for attribute.

Hi Matthew

Thanks for explanation. Though I have no idea of scanning, now I know it should be a best practice to link for attribution of label with correspondent input.

1 Like