Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of the for attribute in HTML?

Tags:

html

I understand the for attribute specifies which form element a label is bound to.

Do you have an example where this would be actually useful?

like image 595
user380719 Avatar asked Dec 01 '22 02:12

user380719


2 Answers

It's most useful for a checkbox label where it will make the whole label clickable so you don't have to target the checkbox itself to toggle its state. Same for radio buttons.

like image 170
Mark Cidade Avatar answered Dec 28 '22 09:12

Mark Cidade


<label for="email">E-mail:</label>
<input type="text" id="email" name="email"/>

Now if you click on "E-mail", the corresponding input element will get focused.

like image 45
rid Avatar answered Dec 28 '22 09:12

rid