label elementlabel elements.formforinterface HTMLLabelElement : HTMLElement {
readonly attribute HTMLFormElement? form;
attribute DOMString htmlFor;
readonly attribute HTMLElement? control;
};
The label represents a caption in a
user interface. The caption can be associated with a specific form
control, known as the label
element's labeled control, either using for attribute, or by putting the form
control inside the label element itself.
Except where otherwise specified by the following rules, a
label element has no labeled control.
The for attribute
may be specified to indicate a form control with which the caption
is to be associated. If the attribute is specified, the attribute's
value must be the ID of a labelable element in the same
Document as the label element. If the attribute is specified and there is an element
in the Document whose ID is equal to the value of the for attribute, and the first such
element is a labelable element,
then that element is the label element's labeled
control.
If the for attribute is not
specified, but the label element has a labelable element descendant, then the
first such descendant in tree order is the
label element's labeled control.
The label element's exact default presentation and
behavior, in particular what its activation behavior
might be, if anything, should match the platform's label behavior.
The activation behavior of a label element
for events targetted at interactive content descendants
of a label element, and any descendants of those
interactive content descendants, must be to do
nothing.
For example, on platforms where clicking a checkbox label checks
the checkbox, clicking the label in the following
snippet could trigger the user agent to run synthetic click
activation steps on the input element, as if
the element itself had been triggered by the user:
<label><input type=checkbox name=lost> Lost</label>
On other platforms, the behavior might be just to focus the control, or do nothing.
The form attribute is used to
explicitly associate the label element with its
form owner.
The following example shows three form controls each with a label, two of which have small text showing the right format for users to use.
<p><label>Full name: <input name=fn> <small>Format: First Last</small></label></p> <p><label>Age: <input name=age type=number min=0></label></p> <p><label>Post code: <input name=pc> <small>Format: AB12 3CD</small></label></p>
controlReturns the form control that is associated with this element.
The htmlFor IDL
attribute must reflect the for content attribute.
The control IDL
attribute must return the label element's labeled
control, if any, or null if there isn't one.
The form IDL attribute is part
of the element's forms API.
labelsReturns a NodeList of all the label
elements that the form control is associated with.
Labelable elements have a
NodeList object associated with them that represents
the list of label elements, in tree order,
whose labeled control is the element in question. The
labels IDL attribute
of labelable elements, on
getting, must return that NodeList object.