Why are the "for" attributes for form forms?

Why are the "for" attributes for form forms? I have never used for them

+4
source share
4 answers

The main advantage is that clicking on the label with the β€œfor” attribute will automatically focus on this form element. Thus, the label for the input field will be associated with this input field, and clicking on the label will autofocus the input.

+8
source
Answer to

@ChristopherArmstrong is technically correct, but the reason is that people who have problems with the right orientation (older users, people with disabilities, etc.) are helped. This allows them to get the "about right" cursor and still land in the desired field.

+3
source

When used with radio buttons, it allows you to click on the shortcut to select the switch:

http://jsfiddle.net/DLL73/

note that clicking on ONE does nothing, because it does not use the for attribute, but clicking on TWO selects this switch.

+3
source

It will associate the label with the form field. This is especially useful for radio buttons, so you can click on the label to select a button, not just the smallest button.

However, for this you do not need to use the for attribute, you can also put a switch in the label:

 <label> <input type="radio" name="selection" value="yes" /> Certainly </label> 

I usually put a span tag around the text so that it can be easily styled using CSS.

+2
source

All Articles