<datalist> Tag in HTML
The <datalist> tag in HTML is used to provide a list of predefined options for an <input> element. When a user begins typing in the input field, it shows a dropdown list of options matching the user's input. This tag is useful for creating suggestions or autocomplete functionality.
Key Points on <datalist> Tag:
- The <datalist> tag works in combination with the <input> tag by linking the input field to the datalist through the list attribute on the input field.
- Each option within the <datalist> is defined with an <option> tag.
- This tag is often used to suggest options without enforcing a fixed list, allowing users to either select from the list or enter their own input.
Syntax of <datalist> Tag:
Syntax Example
<input list="datalist-id">
<datalist id="datalist-id">
<option value="Option 1">
<option value="Option 2">
<option value="Option 3">
</datalist>
Example of <datalist> Tag in HTML:
This example demonstrates an input field that provides a list of programming languages as suggestions using the <datalist> tag.
Code Example
<label for="language">Choose a programming language:</label>
<input list="languages" id="language" name="language">
<datalist id="languages">
<option value="Python">
<option value="JavaScript">
<option value="Java">
<option value="C++">
<option value="Ruby">
</datalist>
Output
The <datalist> tag is ideal for providing suggestions to users while allowing flexibility in their input. Note that the list of options within the datalist is optional and not restrictive, as users can enter their own text even if it’s not in the list.