Open In App

How to specify a list of pre-defined options for input controls in HTML?

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Specify a list of pre-defined options for input controls by using the HTML list Attribute. A pre-defined option for input controls using HTML5's <datalist> element, allowing users to select from a predefined list while typing in an input field.

Syntax:

<input list="datalist_id"/>

Example 1: The example below shows, the HTML form with an input field connected to a datalist providing predefined options for selecting car names.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>
        How to specify a list of pre-defined
          options for input controls?
    </title>
</head>

<body>
    <h1 style="color:green">
        How to specify a list of pre-defined
          options for input controls?

    </h1>

    <form action="">
        <label>Your Cars Name: </label>
        <input list="cars">
        <datalist id="cars">
            <option value="BMW" />
            <option value="Bentley" />
            <option value="Mercedes" />
            <option value="Audi" />
            <option value="Volkswagen" />
        </datalist>
    </form>
</body>

</html>

Output:

pre-defined
Output

Example 2: The example below shows, the HTML form with an input field linked to a datalist of predefined options for language selection.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>
        List of pre-defined options for input controls
    </title>
</head>

<body>
    <h2>
        List of pre-defined options for input controls

    </h2>
    <input list="Languages" />
    <datalist id="Languages">
        <option value="PHP">
        <option value="ASP">
        <option value="Python">
        <option value="Ruby">
        <option value="Java">
    </datalist>
</body>

</html>

Output:

inputpre-defined-options
Output

Next Article

Similar Reads