<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:

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.