for
for attribute
The for
attribute in HTML is used with <label>
and <output>
elements to link them to a specific form control, such as <input>
or <textarea>
. This connection is made by matching the for
attribute's value to the id
of the related element.
Supported Tags
Syntax
<tagname for="value">
Example
<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</form>
Definition and Usage
When used with the <label>
element, the for attribute links the label to a specific form element.
When used with the <output>
element, the for attribute defines the connection between the calculation result and the elements involved in the calculation.
Value
- value
- The value specifies the unique identifier for the attribute, matching the id of the form control (such as
<input>
,<select>
,<textarea>
, etc.). It is always enclosed in quotes.
- The value specifies the unique identifier for the attribute, matching the id of the form control (such as
Applies to
The for
attribute can be applied to the following elements:
Conclusion
The for
attribute in HTML associates a <label>
or <output>
element with a specific form control, improving accessibility. It links the label to an input by matching the for value with the element's id
. This enables users to quickly focus on form inputs by clicking on the labels.
enctype
The enctype attribute in HTML forms defines how the form data should be encoded when sent to the server. This is especially important for forms that handle file uploads or need to ensure special characters are processed correctly.
form
The form attribute in HTML links an input or form element to a separate form, allowing elements to be positioned independently while still being submitted as part of the same form.