I was wondering what the differences are between Select-Option and Datalist-Option. Is there any situation in which it would be better to use one or the other? An example
Datalist includes autocomplete and suggestions natively, it can also allow a user to enter a value that is not defined in the suggestions.
Select only gives you pre-defined options the user has to select from
To specifically answer a part of your question "Is there any situation in which it would be better to use one or the other?", consider a form with repeating sections. If the repeating section contains many select
tags, then the option
s must be rendered for each select, for every row.
In such a case, I would consider using datalist
with input
, because the same datalist
can be used for any number of input
s. This could potentially save a large amount of rendering time on the server, and would scale much better to any number of rows.
Think of it as the difference between a requirement and a suggestion. For the select
element, the user is required to select one of the options you've given. For the datalist
element, it is suggested that the user select one of the options you've given, but he can actually enter anything he wants in the input.
Edit 1: So which one you use depends upon your requirements. If the user must enter one of your choices, use the select
element. If the use can enter whatever, use the datalist
element.
Edit 2: Found this tidbit in the HTML Living Standard: "Each option element that is a descendant of the datalist element...represents a suggestion."
Its similar to select, But datalist has additional functionalities like auto suggest. You can even type and see suggestions as and when you type.
User will also be able to write items which is not there in list.
Data List is a new HTML tag in HTML5 supported browsers. It renders as a text box with some list of options. For Example for Gender Text box it will give you options as Male Female when you type 'M' or 'F' in Text Box.
<input list="Gender">
<datalist id="Gender">
<option value="Male">
<option value="Female>
</datalist>
There is another important difference between select
and datalist
.
Here comes the browser support factor.
select is widely supported by browsers compared to datalist. Please take a look at this page for complete browser support of datalist--
Datalist browser support
Where as select is supported in effectively all browsers (since IE6+, Firefox 2+, Chrome 1+ etc)