HTML option tag

<option> tag in HTML

The <option> tag represents an option in a <select> tag or in a <datalist> tag.

The behavior of this element varies slightly according to, in which of these two elements it's present on.

Example 1 - Under the <select> tag

Try Yourself

Example 2 - Under the <datalist> tag

Try Yourself

attributes of <option> tag


selected attribute in <option> tag

selected attribute is used to set a value as a default in the dropdown list box.

Example

Try Yourself

In the above example Mango will be selected by default.


disabled attribute in <option> tag

This attribute is used to disable the value in the dropdown list box.

Example

Try Yourself

In the above example, you cannot to select the Mango option.


value attribute in <option> tag

This attribute is used to assign a unique value to each list items.

This attribute value can be accessed using javascript.

Example 1

Try Yourself

In the below example we have just set an value that will be used in javascript.

If you feel this difficult to understand leave it, we will see it in upcoming chapters.

Example 2 - Access the dropdown value using javascript

Try Yourself