What is the < \ datalist > tag used for?
Used to provide a "autocomplete" feature in form elements. It allows you to provide a list of predefined options to the user as they enter data.
For example, if a user starts inserting some text into a textfield, a list would be suspended with pre-filled values that they could choose.
The <datalist>
tag can be used in conjunction with a <input>
element that contains a list
attribute.
An example of a <input>
element with default values in a <datalist>
:
<form action="formulario_cor.php" method="post">
<label for="cor">Qual sua cor favorita? </label>
<input list="cor" name="cor" >
<datalist id="cor">
<option value="Vermelho">
<option value="Rosa">
<option value="Amarelo">
<option value="Verde">
<option value="Azul">
<option value="Preto">
</datalist>
<input type="submit" />
</form>
Is this tag new?
According to w3schools documentation , the <datalist>
tag is new to HTML5.
Somehow, it can replace the functionality of
"autocomplemento" that currently exist in several
libraries / Javascript frameworks?
Thanks to the Datalist tag, it will no longer be necessary to use jQuery
or some other JavaScript
framework, along with some server-side code to implement the autocompletion functionality in the inputs, to look for or to fill data in forms, which commonly he is triggered when the user types a letter.
If, for example, we have an entry where the user must include his / her country (as in the example above), we can add the list by datalist element, which provides users with certain suggestions filled or limited only to countries that are accepted for registration.