HTML <select> data-* Attribute

The data-* attribute on a <select> tag attaches additional data to the dropdown control. To create a custom attribute, replace the * with a lowercase string, such as data-id, data-status, or data-location.

Example

#

A custom data-label attribute on a <select> element.
The attribute value is not visible, but it is readable by JavaScript.

<select data-label="Options for city">
  <option value="">-- Select city -- </option>
  <option value="paris">Paris</option>
  <option value="london">London</option>
  <option value="athens">Athens</option>
  <option value="madrid">Madrid</option>
</select>

Using data-*

The data-* attribute adds custom information to a <select> element.

The * part is replaced with a lowercase string, such as data-id, data-cost, or data-location.

An <select> element can have any number of data-* attributes, each with their own name.

Using data-* attributes reduces the need for requests to the server.

Note: The data-* attribute is not visible and does not change the appearance of the select.


Syntax

<select data-*="value">

Note: The * can be any string, such as data-iddata-costdata-supplier,  etc.


Values

#

Value Description
value A string value. Can be numeric, alphanumeric, JSON, etc.

More Examples

A custom data-content attribute on a <select> element.
Clicking the button will display content value.



<select id="myselect" data-content="Cities in Europe">
  <option value="">-- Select city -- </option>
  <option value="paris">Paris</option>
  <option value="london">London</option>
  <option value="athens">Athens</option>
  <option value="madrid">Madrid</option>
</select>

<br/><br/>
<button onclick="show();">Show data</button>

<script>
  let show = () => {
     let element = document.getElementById("myselect");
     alert("Content = " + element.getAttribute('data-content'));
  }
</script>

Code explanation

The <select> tag has a custom data-content attribute.

The data-content attribute stores the description of the <select>.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the <select> content.

Note: Notice how the label displays immediately without server call.


Browser support

Here is when data-* support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <select>
Guides