![]() |
VOOZH | about |
The HTML <option> tag is a versatile tool that enhances user interaction by creating selectable options within a dropdown list. This tag can be used with or without any attributes, and the selected value can be sent to the server.
This elements must be inside <select> tag, <optgroup>, or <datalist> tags. It represents an individual option within a <select> dropdown, allowing users to choose among multiple choices.
<option> Contents... </option>The <option> tag supports several attributes that provide additional functionality and customization. Below are the key attributes and their usage:
Attribute | Value | Description |
|---|---|---|
Disabled | This attribute contains the value disabled which represents the option is disabled. | |
text | This attribute contains the text value which represents the shorted label for the option. | |
selected | This attribute contains the value selected which represents the item that is pre-selected when the browser loaded. | |
text | This attribute contains the value text sent to the server. |
Note: All the attributes mentioned are optional. but its recommended to add value attribute.In addition to the core attributes, <option> elements can also use global and event attributes. These enhance the interactivity and accessibility of the option elements.
<option>. Common ones for <option> include id, class, style, title, disabled, hidden, and custom data-* attributes.<option> element. Common event attributes include onclick, onfocus, onchange, onmousedown, onmouseup, etc. The following example shows a basic dropdown list where users can select an option:
Output:
Bonus Tip: In many scenarios, such as when you create a dropdown for countries where the list grows longer, you can use the <optgroup> tag to group related options.
<optgroup>This example demonstrates how to group related options within a dropdown using the <optgroup> tag:
Output: