The datalist
tag is used to provide an auto-complete option for users through a dropdown menu. It is used together with the input
tag in forms.
The datalist
tag has a single attribute: an id
.
The
id
attribute ofdatalist
must match thelist
attribute of theinput
tag. This ensures that the dropdown menu is related to the input field.
The illustration below shows how thedatalist
tag can be used in HTML:
The syntax of datalist
tag is as follows:
<datalist id="some-id">
<option value="A">
<option value="B">
<option value="C">
</datalist>
We begin by opening the tags using the <>
symbol. In between the symbol, we write datalist
. We then close the tags using </>
. Once again, we will place the name of our tag in-between <>
and after the /
symbol.
The id
acts as an identifier of the datalist
tag. Each option inside the dropdown menu comes inside the option
tag. The option
tag is also enclosed within the <>
symbol. Finally, each option
has a value
attribute that holds each value inside the dropdown menu.
The code snippet below shows how the datalist
tag can be used in HTML
:
Names of all countries have not been added to the list.
label
tag places a label next to the input field.input
tag creates a textbox for user input.datalist
tag can have as many option
tags as possible. Each option
tag has a value
that stores a country name.id
attribute of the datalist
tag is the same as the list
attribute of the input
tag.Free Resources