views:

55

answers:

3

Hello,

In HTML, What's the purpose of the attribute for in the label tag.

<label for = "Name">Name: </label>
<% = Html.TextBox("Name")%>

In the above sample, why the label tag has a for = "Name" attribute? To mark their association??? If I don't put for what gonna happen?

Thanks for helping

+4  A: 

The for attribute specifies which form element the label is bound to.

The label element allows the user to give focus to a form element by clicking on an associate label. If you do not use the for attribute, this association will not be made.

Daniel Vassallo
+2  A: 

If you click on the label, you may for instance check or uncheck a checkbox / radio button. The for attribute tells what checkbox the label belongs to.

A complete example is available at http://www.w3schools.com/tags/tag_label.asp.

aioobe
+2  A: 

If the user clicks on the label, and the for attribute matches that id of the respective control, it will toggle the control.

gmcalab