Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using the Datalist Element for Predefined Input Suggestions | HTML Forms and User Input
Ultimate HTML

book
Using the Datalist Element for Predefined Input Suggestions

The <datalist> element in HTML is used to create a predefined list of options for an <input> element. It allows the user to select an option from the list while also giving them the ability to enter their own value if they choose. The list of predefined values remains hidden until the user starts typing in the associated text field. The <datalist> is paired with an id attribute, and the <input> is linked to it using the list attribute.

html

index.html

copy
<form onsubmit="return false">
<label for="country">My country is</label>
<input type="text" name="country" id="country" list="countries" />
<datalist id="countries">
<option value="Canada">Canada</option>
<option value="Australia">Australia</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
<option value="New Zealand">New Zealand</option>
</datalist>
<button type="submit">Submit</button>
</form>

Overall, the <datalist> element can be a helpful way to provide a pre-defined list of options for users to select from, while still allowing them the flexibility to enter their own value if needed.

Example

When the user starts typing in the category input field, the browser will display the predefined categories from the datalist. If none of the options match the user's input, they can still type their own category. This allows the user to choose from a list and manually input a value if necessary.

html

index.html

copy
<form onsubmit="return false">
<label for="category">Select a product category</label>
<input type="text" name="category" id="category" list="categories" />
<datalist id="categories">
<option value="Electronics"></option>
<option value="Clothing"></option>
<option value="Home Appliances"></option>
<option value="Books"></option>
<option value="Toys"></option>
</datalist>
<button type="submit">Submit</button>
</form>
  • <label for="category">: labels the input field, indicating to the user that they should select or enter a product category;

  • <input type="text" name="category" id="category" list="categories"/>: the input field where the user can either type a category or select from the available options. The list="categories" attribute links this input to the datalist with the ID categories;

  • <datalist id="categories">: this contains a list of predefined product categories. These options will appear as suggestions when the user starts typing in the input field;

  • <option value="...">: each option represents a product category in the list. The user can select one of these categories or type their own value if it doesn't match any of the options.

1. Which HTML element is used to create a drop-down list with multiple options?

2. What is the purpose of the datalist element?

3. What attribute is used to link a datalist element to an input element?

question mark

Which HTML element is used to create a drop-down list with multiple options?

Select the correct answer

question mark

What is the purpose of the datalist element?

Select the correct answer

question mark

What attribute is used to link a datalist element to an input element?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 9

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

We use cookies to make your experience better!
some-alt