<datalist> and <input type="list">
The <datalist>
and <input type="list">
elements are used in HTML5 to create a list of predefined options for an input field. The <datalist>
element provides a list of options to suggest to the user as they type, while the <input type="list">
element creates a dropdown list of options for the user to choose from.
Here's an example of how you can use these elements:
<label for="browser">Choose a web browser:</label>
<input list="browsers" name="browser" id="browser">
<datalist id="browsers">
<option value="Google Chrome">
<option value="Mozilla Firefox">
<option value="Apple Safari">
<option value="Microsoft Edge">
</datalist>
In this example, a label and an input field are created. The input field is linked to a datalist using the list
attribute, and the options for the datalist are defined within the <datalist>
element. When the user starts typing in the input field, the browser will display a list of options that match the characters typed, based on the values in the datalist.