Path: blob/main/files/en-us/web/html/element/datalist/index.md
6546 views
------{{HTMLSidebar}}
The <datalist> HTML element contains a set of {{HTMLElement("option")}} elements that represent the permissible or recommended options available to choose from within other controls.
{{EmbedInteractiveExample("pages/tabbed/datalist.html", "tabbed-standard")}}
To bind the <datalist> element to the control, we give it a unique identifier in the id attribute, and then add the list attribute to the {{HTMLElement("input")}} element with the same identifier as value. Only certain types of {{HTMLElement("input")}} support this behavior, and it can also vary from browser to browser.
Note: The
<option>element can store a value as internal content and in thevalueandlabelattributes. Which one will be visible in the drop-down menu depends on the browser, but when clicked, content entered into control field will always come from thevalueattribute.
Attributes
This element has no other attributes than the global attributes, common to all elements.
Examples
Textual types
Recommended values in types {{HTMLElement("input/text", "text")}}, {{HTMLElement("input/search", "search")}}, {{HTMLElement("input/url", "url")}}, {{HTMLElement("input/tel", "tel")}}, {{HTMLElement("input/email", "email")}} and {{HTMLElement("input/number", "number")}}, are displayed in a drop-down menu when user clicks or double-clicks on the control. Typically the right side of a control will also have an arrow pointing to the presence of predefined values.
{{EmbedLiveSample("Textual_types", 600, 40)}}
Date and Time types
The types {{HTMLElement("input/month", "month")}}, {{HTMLElement("input/week", "week")}}, {{HTMLElement("input/date", "date")}}, {{HTMLElement("input/time", "time")}} and {{HTMLElement("input/datetime-local", "datetime-local")}} can show an interface that allows a convenient selection of a date and time. Predefined values can be shown there, allowing the user to quickly fill the control value.
Note: When type is not supported,
texttype creating simple text field will be used instead. That field will correctly recognize recommended values and display them to the user in a drop-down menu.
{{EmbedLiveSample("Date_and_Time_types", 600, 40)}}
Range type
The recommended values in the {{HTMLElement("input/range", "range")}} type will be shown as series of hash marks that the user can easily select.
{{EmbedLiveSample("Range_type", 600, 70)}}
Color type
The {{HTMLElement("input/color", "color")}} type can show predefined colors in a browser-provided interface.
{{EmbedLiveSample("Color_type", 600, 70)}}
Password type
The specification allows linking <datalist> with a {{HTMLElement("input/password", "password")}} type, but no browser supports it for security reasons.
{{EmbedLiveSample("Password_type", 600, 40)}}
Technical summary
| Content categories | Flow content, phrasing content. |
|---|---|
| Permitted content | Either phrasing content or zero or more {{HTMLElement("option")}} elements. |
| Tag omission | {{no_tag_omission}} |
| Permitted parents | Any element that accepts phrasing content. |
| Implicit ARIA role | listbox |
| Permitted ARIA roles | No role permitted |
| DOM interface | {{domxref("HTMLDataListElement")}} |
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
The {{HTMLElement("input")}} element, and more specifically its {{htmlattrxref("list", "input")}} attribute;
The {{HTMLElement("option")}} element.