HTML list Attribute
Description
The HTML list attribute is associated with the <input> element and is used to bind the input field to a <datalist> element. This association enables the creation of a predefined list of suggested values for the input field, enhancing user experience by providing an autocomplete feature. When users start typing in the input field, the browser displays a dropdown list of options that match the users' input, allowing them to select a suggested value instead of typing the entire value themselves. This is particularly useful for fields that have a limited set of possible values, such as country names, product names, or other categories.
The list attribute's value must be the same as the id attribute of the <datalist> element it is associated with. The <datalist> element contains a number of <option> elements, each representing a suggested value for the input field.
Here's a simple example of how to use the list attribute:
<input type="text" id="ice-cream-choice" name="ice-cream-choice" list="ice-cream-flavors" />
<datalist id="ice-cream-flavors">
<option value="Chocolate">
<option value="Coconut">
<option value="Mint">
<option value="Strawberry">
<option value="Vanilla">
</datalist>
In this example, when the user starts typing in the input field, they will see a dropdown list of ice cream flavors to choose from. This not only speeds up the form filling process but also helps prevent typing errors and ensures the consistency of the input data.
Syntax
<input list="datalist-id">
Values
- datalist-idThe
idof the <datalist> element.
Applies To
Example
Browser Support
The following information will show you the current browser support for the HTML list attribute. Hover over a browser icon to see the version that first introduced support for this HTML attribute.
Browser support for this attribute varies across browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 28th March 2024
