Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <datalist> id Attribute

An id on a <datalist> tag assigns an identifier to the 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 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.


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>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides