tags:

views:

39

answers:

2
  <label id="label1" for="txtTextBox">

what is the impact of label1 if I put the for attribute in there?

+1  A: 

It refers to the id (not name!) of the form element (input, select, textarea, option, etc.) that it is labelling. The implication of this is that using for allows one to click on the label, and have the related form element focused.

Delan Azabani
It refers to the id not the name.
Michael Shimmins
That's what I said, though.
Delan Azabani
My bad - read it as "It refers to the name of the form..."
Michael Shimmins
+5  A: 

It allows you to create a label that is attached to a specific element in the DOM. When the label receives a mouse down event it focuses the element it is attached to.

<label for="username">Username:</label>
<input type="text" id="username" name="username" />

When the user clicks on the "Username:" text it will focus the text box.

This is also good for accessibility as screen readers will read the label's inner HTML before reading the input field, regardless of the physical order they appear in the DOM.

Michael Shimmins