lynn
lynn

Reputation: 2958

What's the "for" for in a label tag?

Just ran across a for parameter in an HTML label tag:

<label for="required-firstname"> First Name </label>
<small>*</small>
<input name="required-firstname" type="text" tabindex="2" 
       id="required-firstname" size="24" maxlength="40">

I'm converting this form to a PHP processed script, can I get rid of the for= parameters? (And out of curiosity, what does it do?)

Upvotes: 84

Views: 71692

Answers (6)

Jonas
Jonas

Reputation: 4584

From w3schools.org:

The tag defines a label for an input element.

The label element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the label element, it toggles the control.

The for attribute of the tag should be equal to the id attribute of the related element to bind them together.

HTH!

adding my $.02 as an Accessibility SME - as well as usability, the LABEL also associates the input field with the correct label so persons using screen readers will know what the field is for.

Upvotes: 118

user3219915
user3219915

Reputation: 81

The "for" attribute is a necessary element for the accessibility of your form. Do not omit it. For someone using a screen reader (SR) to have a web page announced to them, the "for" attribute relates the control to the label. Typically a SR user will tab through a form, from one control (which is a focusable element for the SR) to the next. Without the "for" attribute, the SR user will have to change modes on the SR and probe around the form to try and determine which control matches which label, which can be time-consuming and confusing. The "for" attribute can also be useful for assistive technology relating to motor issues.

WebAIM.org has a great page explaining the accessibility ramifications of "for": http://webaim.org/techniques/forms/controls

Upvotes: 8

Pawel Krakowiak
Pawel Krakowiak

Reputation: 10090

It specifies to which element that label is bound to. In your sample code the label is there for the required-firstname input field. If the user clicks on that label, the focus will go to the bound input field. It's a usability improvement and I think you'd be better off leaving it as is. It's a good practice.

Upvotes: 9

Bill the Lizard
Bill the Lizard

Reputation: 405745

The HTML label tag defines a label for a form element. They're usually used with checkboxes and radio buttons, and when the user clicks on the label it toggles the button. With a text input (and you'll have to check this to be sure) I think it only gives focus to the input when the user clicks the label.

Upvotes: 23

Peter Turner
Peter Turner

Reputation: 11435

In some browsers when you click on a text in a for tag, you'll check the box it's associated with (i.e. the for = id) or put the focus on that box. It's an ADA thing

Upvotes: 3

VirtuosiMedia
VirtuosiMedia

Reputation: 53356

It ties the label to a form element id. Some form elements, like checkboxes, can be activated by clicking on their label.

Upvotes: 8

Related Questions