HTML <option> Tag

The HTML <option> tag represents an option in a <select> element or as part of a list of suggestions in a <datalist> element.

The <option> tag can be used as a child of a <select>, <datalist>, and a <optgroup> element.

Syntax

The <option> tag is written as <option></option> with the element's content inserted between the start and end tags.

The value attribute is normally used to provide a value that can be referenced by any script that processes the form.

Like this:

Examples

Basic tag usage

Here, the <option> tag is used with a <select> tag to present a list of options that the user can choose from.

The selected Attribute

Here, we use the selected attribute to ensure that an option is the default selection (i.e. it is already selected as soon as the page loads).

In this case, we specify that Chiang Mai is the default selection.

Using the <optgroup> Element

Items within a <option> element can be sorted into logical groups. You can do this using the <optgroup> element.

Here, we group a list of cities by country.

Using the <datalist> Element

The <option> tag can be used with the <input> and <datalist> tags to provide a list of suggestions for the user. These aren't necessarily all available options, just a mere list of suggestions.

Try typing the letter "B" into the field below. Then try "H". Then try say, "M".

Your browser may also provide a means for seeing all available options. However, there's nothing to stop you from entering an option that's not on the list.

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <option> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <option> tag/element.

AttributeDescription
disabledDisables the input control. The button won't accept changes from the user. It also cannot receive focus and will be skipped when tabbing.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either disabled or disabled="disabled").

Possible values:

  • [Empty string]
  • disabled
labelSpecifies a label to be used as an alternative to the option item's contents. Useful if you'd prefer a shorter, more concise label.
selectedSpecifies that this option will be pre-selected when the user first loads the page.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either selected or selected="selected").

Possible values:

  • [Empty string]
  • selected
valueSpecifies the initial value of the option item.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <option> tag , as well as with all other HTML tags.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handler Content Attributes

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <option> element, as well as any other HTML5 element.

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

None. However, the <option> can now appear inside a <datalist> tag (which wasn't available in HTML 4).

To see more detail on the two versions see HTML5 <option> Tag and HTML4 <option> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <option> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <option> Tag and HTML4 <option> Tag.

Tag Details

For more details about the <option> tag, see HTML5 <option> Tag and HTML4 <option> Tag.

Specifications

Here are the official specifications for the <option> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.