for (HTML attribute)
Example
This for
attribute points to a form control with the
id of "b":
<input type="radio" name="radFriendship" value="Not_Applicable"
id="b"/>
<label for="b" accesskey="b">Not_Applicable</label>
Description
The for
attribute is the glue that binds the text contained inside the
label element to the form control to which it relates.
There’s another way that descriptive text can be linked to a form control:
instead of using a for attribute, the opening
<label> and closing </label> tags can be
wrapped around the form control. In that instance, the example code would
look like this:
<label accesskey="b"><input type="radio" name="radFriendship"
value="Not_Applicable" id="b"/> Not_Applicable</label>
Finally, you could take the belt-and-braces approach and do
both: wrap the label around the form control
and link it using the for
attribute:
<label for="b" accesskey="b"><input type="radio"
name="radFriendship" value="Not_Applicable" id="b"/>
Not_Applicable</label>
Note that the former method—using the
for attribute—is by far the safer option (at least
one screen reader, Window Eyes, is not able to make the correct
association when the label is wrapped around the
related text).
Value
This attribute takes as its value a
reference to an existing (and valid) id of a form
control.
Compatibility
| IE | 5.5 | Full |
|---|---|---|
| 6.0 | Full | |
| 7.0 | Full | |
| Firefox | 1.0 | Full |
| 1.5 | Full | |
| 2.0 | Full | |
| Safari | 1.3 | None |
| 2.0 | None | |
| 3.0 | Full | |
| Opera | 9.2 | Full |
The
for attribute is more widely supported than the
approach that involves simply wrapping the label around
the control, and is the preferred method. It’s also more flexible, as the
text and control may be separated by other HTML structural elements that
would make it impossible to wrap the inline label
around the control and the associated text.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

