Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are "for" attributes for form labels necessary?

Tags:

html

forms

label

Why are "for" attributes for form labels necessary? I've never had a use for them

like image 928
Yarin Avatar asked Dec 05 '22 21:12

Yarin


2 Answers

The main advantage is that clicking on a label with a "for" attribute will auto-focus on that form element. So, a label for an input field will be associated with that input field, and clicking on the label will autofocus the input.

like image 130
Christopher Armstrong Avatar answered Dec 27 '22 15:12

Christopher Armstrong


@ChristopherArmstrong's answer is technically correct - but the reason it's a good thing is that people who have trouble pointing correctly (older users, people with disabilities, etc.) are helped by this. It lets them get the cursor "about right" and still land in the right field.

like image 45
Scott C Wilson Avatar answered Dec 27 '22 16:12

Scott C Wilson