Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "for" attribute do in HTML <label> tag?

I wonder what is the difference between the following two code snippets:

<label>Input here : </label> <input type='text' name='theinput' id='theinput'/> 

and

<label for='theinput'>Input here : </label> <input type='text' name='theinput' id='theinput'/> 

I'm sure it does something when you use a special JavaScript library, but apart from that, does it validate the HTML or required for some other reason?

like image 475
jeff Avatar asked Aug 25 '13 18:08

jeff


People also ask

What is the for attribute used for in HTML?

The for attribute is an allowed attribute for <label> and <output> . When used on a <label> element it indicates the form element that this label describes.

Do you need a for attribute on label?

To associate the <label> with an <input> element, you need to give the <input> an id attribute. The <label> then needs a for attribute whose value is the same as the input's id .

Why should the value of the for attribute of label tag?

Description. The objective of this technique is to use the label element to explicitly associate a form control with a label. A label is attached to a specific form control through the use of the for attribute. The value of the for attribute must be the same as the value of the id attribute of the form control.


2 Answers

The <label> tag allows you to click on the label, and it will be treated like clicking on the associated input element. There are two ways to create this association:

One way is to wrap the label element around the input element:

<label>Input here:     <input type='text' name='theinput' id='theinput'> </label> 

The other way is to use the for attribute, giving it the ID of the associated input:

<label for="theinput">Input here:</label> <input type='text' name='whatever' id='theinput'> 

This is especially useful for use with checkboxes and buttons, since it means you can check the box by clicking on the associated text instead of having to hit the box itself.

Read more about this element in MDN.

like image 57
Barmar Avatar answered Oct 10 '22 22:10

Barmar


The for attribute associates the label with a control element, as defined in the description of label in the HTML 4.01 spec. This implies, among other things, that when the label element receives focus (e.g. by being clicked on), it passes the focus on to its associated control. The association between a label and a control may also be used by speech-based user agents, which may give the user a way to ask what the associated label is, when dealing with a control. (The association may not be as obvious as in visual rendering.)

In the first example in the question (without the for), the use of label markup has no logical or functional implication – it’s useless, unless you do something with it in CSS or JavaScript.

HTML specifications do not make it mandatory to associate labels with controls, but Web Content Accessibility Guidelines (WCAG) 2.0 do. This is described in the technical document H44: Using label elements to associate text labels with form controls, which also explains that the implicit association (by nesting e.g. input inside label) is not as widely supported as the explicit association via for and id attributes,

like image 27
Jukka K. Korpela Avatar answered Oct 10 '22 22:10

Jukka K. Korpela