189

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 of each follows:

Select-Option

<select name="browser">
<option value="firefox">Firefox</option>
<option value="ie">IE</option>
<option value="chrome">Chrome</option>
<option value="opera">Opera</option>
<option value="safari">Safari</option>
</select>

Datalist-Option

<input type="text" list="browsers">
<datalist id="browsers">
  <option value="Firefox">
  <option value="IE">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>
james.garriss
  • 12,959
  • 7
  • 83
  • 96
user928984
  • 1,963
  • 2
  • 14
  • 13
  • 13
    Because the HTML5 specifically says that unquoted attributes are valid: http://www.w3.org/TR/html-markup/syntax.html#syntax-attr-unquoted – james.garriss Jan 05 '16 at 15:57
  • 3
    Anyone know why we don't close the `option` tag in the datalist-option? Sublime seems to want to – Johnny Metz Nov 17 '16 at 23:29
  • 1
    @ johnny Metz You can close the tag but it can be self closing you can also do something like this: `code` `code` The result is strange. Datalists print the value in the list. That value then becomes the value of the input field. – Sarah M Giles May 24 '18 at 20:04
  • 3
    @JohnnyMetz, HTML 5 was, in part, a reaction against XHTML. For certain elements, like `option`, there is no need to have a closing tag or to be self-closing. HTML 5 != XHTML. – james.garriss Sep 05 '18 at 12:21
  • 2
    The spec says, "The start and end tags of certain normal elements can be omitted." It also says, "An option element’s end tag may be omitted if the option element is immediately followed by another option element, or if it is immediately followed by an optgroup element, or if there is no more content in the parent element." https://www.w3.org/TR/html/syntax.html#optional-tags – james.garriss Sep 05 '18 at 12:27

9 Answers9

254

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."

james.garriss
  • 12,959
  • 7
  • 83
  • 96
  • Also, it has mostly [partial support]((http://caniuse.com/#feat=datalist) in the other browsers, with bugs such as long datalists becoming unscrollable, etc. – Govind Rai Sep 26 '16 at 00:34
  • In chrome at the moment, if data is entered (typed) it prohibits the arrow from being clicked. This is probably not ideal in most cases. – David May 24 '20 at 18:18
87

From a technical point of view they're completely different. <datalist> is an abstract container of options for other elements. In your case you've used it with <input type="text" but you can also use it with ranges, colors, dates etc. http://demo.agektmr.com/datalist/

If using it with text input, as a type of autocomplete, then the question really is: Is it better to use a free-form text input, or a predetermined list of options? In that case I think the answer is a bit more obvious.

If we focus on the use of <datalist> as a list of options for a text field then here are some specific differences between that and a select box:

  1. A <datalist> fed text box has a single string for both display label and submit. A select box can have a different submit value vs. display label <option value='ie'>Internet Explorer</option>.

  2. A <datalist> fed text box does not support the <optgroup> tag to organize the display.

  3. You can not restrict a user to the list of options in a <datalist> like you can with a <select>.

  4. The onchange event works differently. On a <select> element, the onchange event is fired immediately upon change, whereas with <input type="text" the event is fired after the element loses focus or the user presses enter.

  5. <datalist> has really spotty support across browsers. The way to show all available options is inconsistent, and things only get worse from there.

The last point is really the big one in my opinion. Since you will HAVE to have a more universal autocomplete fallback, then there is almost no reason to go through the trouble of configuring a <datalist>. Plus any decent autocomplete pluging will allow for ways to style the display of your options, which <datalist> does not do. If <datalist> accepted <li> elements that you could manipulate however you want, it would have been really great! But NO.

Also insofar as i can tell, the <datalist> search is an exact match from the beginning of the string. So if you had <option value="internet explorer"> and you searched for 'explorer' you would get no results. Most autocomplete plugins will search anywhere in the text.

I've only used <datalist> as a quick and lazy convenience helper for some internal pages where I know with a 100% certainty that the users have the latest Chrome or Firefox, and will not try to submit bogus values. For any other case, it's hard to recommend the use of <datalist> due to very poor browser support.

Kalnode
  • 9,386
  • 3
  • 34
  • 62
mastaBlasta
  • 5,700
  • 1
  • 24
  • 26
  • 3
    Excellent answer!! Could you please explain your second bullet? What do you mean by "option groups to organize the display"? Thanks. – Govind Rai Sep 26 '16 at 06:34
  • afaik (in 2019, on Chrome and Firefox), matches infixes (not just prefixes). so "typing "re" suggests both "Firefox" and "Internet Explorer". – sam boosalis Feb 10 '19 at 18:20
  • @GovindRai I cursory search of "optgroup element" returns this page from the always-useful MDN with more information (and examples): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup – TylerH Mar 28 '19 at 16:31
  • 2
    I have tested under Firefox 66 and Chrome 73 - actually, datalist labels match anywhere, so for example if your datalist contains country names, "ted" will match "United States" and "United Kingdom". – Błotosmętek May 10 '19 at 08:06
  • 1
    the situation is different going into 2023. 95% of browsers running around now support it. FireFox only supports text right now but they are also moving to support the full range, and FireFox is not nearly as common as it was five years ago. Over 80% of the browser market is Chrome or Safari with Microsoft Edge being a little more common in businesses. All three of these browsers no support a much more advanced search inside of datalist. search has improved dramatically, so external plugins aren't needed. SelectList can also fit inside of a DataList if you do some finagling. – jdmneon Dec 04 '22 at 18:01
12

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

user3167654
  • 263
  • 1
  • 4
  • 9
  • the situation is different going into 2023. 95% of browsers running around now support it. FireFox only supports text right now but they are also moving to support the full range, and FireFox is not nearly as common as it was five years ago. Over 80% of the browser market is Chrome or Safari with Microsoft Edge being a little more common in businesses. All three of these browsers no support a much more advanced search inside of datalist. search has improved dramatically, so external plugins aren't needed. SelectList can also fit inside of a DataList if you do some finagling. – jdmneon Dec 04 '22 at 18:01
3

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 type="text" list="Gender">
<datalist id="Gender">
  <option value="Male">
  <option value="Female"> 
</datalist>
AlirezaAP
  • 25
  • 5
Deepak
  • 65
  • 3
  • 8
    True, but it does give one nice, new piece of information, namely that typing the first letter will select corresponding items in the list. – james.garriss Jan 20 '15 at 23:01
2

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 options 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 inputs. This could potentially save a large amount of rendering time on the server, and would scale much better to any number of rows.

Bruce Pierson
  • 567
  • 7
  • 11
  • 1
    If you're not going to bother giving the user options to choose from, why bother using a datalist? Just use a text field instead. – james.garriss Jan 22 '16 at 20:53
2

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.

VIJ
  • 1,516
  • 1
  • 18
  • 34
1

In the 'select' element, there is some kind of compulsion, that the user had to choose one option among other options, but the 'datalist' shows the freedom to enter any option the user wants, either from the suggested options or anything else she/he wants.

Samsi
  • 11
  • 3
0

I noticed that there is no selected feature in datalist. It only gives you choice but can't have a default option. You can't show the selected option on the next page either.

Weihui Guo
  • 3,669
  • 5
  • 34
  • 56
  • The equivalent for an input plus datalist would be setting the value="(default option)" on the input itself. For an input with type="text", this text will appear by default but be editable. – Bemisawa Jul 18 '17 at 18:38
0

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)

neophyte
  • 6,540
  • 2
  • 28
  • 43
  • `` is also widely supported. Take a look: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist#browser_compatibility – Beki Jun 26 '22 at 00:44