Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding the HTML Label's "For" Property [duplicate]

Tags:

html

label

Considering the following 2 lines of code (copied from w3schools.com > "HTML < label > for Attribute"):

  <label for="male">Male </label>   <input type="radio" name="sex" id="male" /> 

I am having trouble discovering the exact purpose of the above label's "for" property. As you can see it is currently set to "male" (to match the id of the input control).

All I have read so far is that the above code will 'associate' and 'bound' the label with the input control. So my question is, what exactly does this mean?

What exactly are the results of associating the label to the input control?
Does the label and/or input have new behaviours as a result of this 'association'?

like image 624
user328414 Avatar asked Jun 29 '10 16:06

user328414


People also ask

What is for property in label HTML?

The for attribute is used in labels. It refers to the id of the element this label is associated with. Now when the user clicks with the mouse on the username text the browser will automatically put the focus in the corresponding input field. This also works with other input elements such as <textbox> and <select> .

What is the for in label HTML?

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.

Why should the value of for attribute of label tag be same as the ID of the corresponding input element?

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. The id attribute may have the same value as the name attribute, but both must be provided, and the id must be unique in the Web page.

Which of the following is the attribute of the label tag?

The <label> tag defines the label for <button>, <input>, <meter>, <output>, <progress>, <select>, or <textarea> element. The <label> tag can be used in two ways: Firstly, use <label> tag by providing the <input> and id attribute. The <label> tag needs a for attribute whose value is the same as input id.


2 Answers

A label that is associated with a control via for will be clickable. Clicking it selects the control. Highly useful with radio/checkboxes in particular. It also has accessibility implications for screen readers for the visually impaired.

like image 135
ceejayoz Avatar answered Oct 14 '22 04:10

ceejayoz


When you click on the label (Male), the radio will be checked something not possible if you are not using a label. A label is also useful when developing for small devices such as mobiles.

So, it is useful for:

  • accessibility reasons
  • smaller devices such as mobiles, etc
  • useful in radio buttons and check boxes especially
like image 44
Sarfraz Avatar answered Oct 14 '22 04:10

Sarfraz