label
elementlabel
elements.form
for
interface 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, either using for
attribute, or by putting the form control inside the label
element itself.
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 form-associated element in the same Document
as the label
element.
control
Returns the form control that is associated with this element.
The form
attribute is used to explicitly associate the label
element with its form owner.
The htmlFor
IDL attribute must reflect the for
content attribute.
labels
Returns a NodeList
of all the label
elements that the form control is associated with.
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>