HTML <datalist> id Attribute

The id attribute on a <datalist> tag assigns an identifier to the data list element. The identifier must be unique across the page.

Example

#

A unique id attribute on a <datalist>.

<datalist id="techlist">
  <option value="HTML">
  <option value="Css">
  <option value="Bootstrap">
  <option value="C#">
</datalist>

<form action="/tutorial/action.html">
  <input type="text" list="techlist" 
         name="technology" placeholder="Enter technology">
  <input type="submit">
</form>

Using id

The id attribute assigns an identifier to the <datalist> element.

The identifier must be unique across the page.

The id allows programmatic access to the <datalist> element.

Tip:  id is a global attribute that can be applied to any HTML element.


Syntax

<datalist id="identifier" >

Values

#

Value Description
identifier A unique alphanumeric string. The id value must begin with a letter ([article-Zarticle-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.).

More Examples

A <datalist> that is bound to an <input> element through a unique id.
Clicking the button displays the datalist content.


<datalist id="techdatalist">
  <option value="HTML">
  <option value="CSS">
  <option value="Bootstrap">
  <option value="C#">
</datalist>

<input type="text" list="techdatalist"
       name="tech" placeholder="Enter a technology">

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

<script>
  let show = () => {
    let element = document.getElementById("techdatalist");
    alert("Datalist content = " + element.innerHTML);
  }
</script>

Code explanation

The id attribute assigns a unique identifier for the datalist.

Clicking the 'Show' button calls JavaScript which locates the datalist through the id.

Finally, the content of the datalist options are displayed in an alert box.


Browser support

Here is when id support started for each browser:

Chrome
20.0 Jun 2012
Firefox
4.0 Mar 2011
IE/Edge
10.0 Sep 2012
Opera
12.1 Nov 2012
Safari
12.1 Mar 2019

You may also like

 Back to <datalist>
Guides