Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the HTML for="" attribute in <label>?

Tags:

html

I have seen this in jQuery - what does it do?

<label for="name"> text </label> <input type="text" name="name" value=""/> 
like image 885
Chris G. Avatar asked Oct 15 '12 10:10

Chris G.


People also ask

What is the for attribute in label element?

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.

What is the HTML for attribute used for?

Definition and Usage When used together with the <label> element, the for attribute specifies which form element a label is bound to. When used together with the <output> element, the for attribute specifies the relationship between the result of the calculation, and the elements used in the calculation.

What is label in HTML code?

The <label> tag in HTML is used to provide a usability improvement for mouse users i.e, if a user clicks on the text within the <label> element, it toggles the control. The <label> tag defines the label for <button>, <input>, <meter>, <output>, <progress>, <select>, or <textarea> element.

What goes in a label in HTML?

<input type="text"> <input type="time"> <input type="url">


2 Answers

The for attribute is used in labels. It refers to the id of the element this label is associated with.

For example:

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

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>.

Quote from the specification:

This attribute explicitly associates the label being defined with another control. When present, the value of this attribute must be the same as the value of the id attribute of some other control in the same document. When absent, the label being defined is associated with the element's contents.

As far as why your question is tagged with jQuery and where did you see it being used in jQuery I cannot answer because you didn't provide much information.

Maybe it was used in a jQuery selector to find the corresponding input element given a label instance:

var label = $('label'); label.each(function() {     // get the corresponding input element of the label:     var input = $('#' + $(this).attr('for')); }); 
like image 140
Darin Dimitrov Avatar answered Sep 23 '22 21:09

Darin Dimitrov


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:

<label for="username">Click me</label> <input type="text" id="username"> 

The for attribute associates a <label> with an <input> element; which offers some major advantages:
1. The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too. This means that, for example, a screen reader will read out the label when the user is focused on the form input, making it easier for an assistive technology user to understand what data should be entered.
2. You can click the associated label to focus/activate the input, as well as the input itself. This increased hit area provides an advantage to anyone trying to activate the input, including those using a touch-screen device.

Alternatively, you can nest the <input> directly inside the <label>, in which case the for and id attributes are not needed because the association is implicit:

<label>Click me <input type="text"></label> 

Notes:
One input can be associated with multiple labels.
When a <label> is clicked or tapped and it is associated with a form control, the resulting click event is also raised for the associated control.

Accessibility concerns

Don't place interactive elements such as anchors or buttons inside a label. Doing so, makes it difficult for people to activate the form input associated with the label.

Headings

Placing heading elements within a <label> interferes with many kinds of assistive technology, because headings are commonly used as a navigation aid. If the label's text needs to be adjusted visually, use CSS classes applied to the <label> element instead.
If a form, or a section of a form needs a title, use the <legend> element placed within a <fieldset>.

Buttons

An <input> element with a type="button" declaration and a valid value attribute does not need a label associated with it. Doing so may actually interfere with how assistive technology parses the button input. The same applies for the <button> element.

Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label

like image 22
wasmup Avatar answered Sep 23 '22 21:09

wasmup