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#">
</datalist>
<form action="/tutorial/action.html">
<input type="text" list="techlist"
name="technology" placeholder="Enter technology">
<input type="submit">
</form>
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 (.). |
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>
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.
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 |
Back to <datalist>