HTML <option> Tag
The <option>
tag in HTML is used to define an option in a <select>
element. The <select>
element is used to create a drop-down list where users can select an option.
Each <option>
tag represents an option in the drop-down list. It must be placed inside a <select>
element.
Example:
<select>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
In the above example, we have three options in the drop-down list: "Option 1", "Option 2", and "Option 3". Each <option>
tag has a value
attribute that specifies the value of the option.
Attributes of the <option>
tag:
Attribute | Description |
---|---|
value | Specifies the value of the option. This is the value that will be sent to the server when the form is submitted. |
selected | Specifies that the option should be pre-selected when the page loads. |
disabled | Specifies that the option should be disabled and cannot be selected. |
label | Specifies a label for the option. This can be used to display a custom label for the option. |
Example with attributes:
<select>
<option value="1" selected>Option 1</option>
<option value="2" disabled>Option 2</option>
<option value="3" label="Three">Option 3</option>
</select>
In the above example, we have set different attributes for each option. The first option is pre-selected, the second option is disabled, and the third option has a custom label "Three".