HTML <datalist> id Attribute

An id on a <datalist> tag assigns an identifier to the element.

The identifier must be unique across the page.



A unique id attribute on a <datalist>.

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

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

Using id

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

The id allows JavaScript to easily access the <datalist> element.

It is also used to point to a specific id selector in a style sheet.

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


<datalist id="identifier" >



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#">

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

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

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

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:

20.0 Jun 2012
4.0 Mar 2011
10.0 Sep 2012
12.1 Nov 2012
12.1 Mar 2019

You may also like

 Back to <datalist>

Last updated on Sep 30, 2023