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

bookUsing 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.

index.html

index.html

copy

The <datalist> element is useful when you want to provide helpful suggestions without limiting users to a fixed set of choices.

Example

When the user types in the field, the browser shows the predefined categories. If none match, the user can still enter their own value.

index.html

index.html

copy
  • <label for="category">: describes the purpose of the input field;
  • <input type="text" name="category" id="category" list="categories"/>: a text field that displays suggestions from a linked datalist;
  • <datalist id="categories">: contains predefined options that appear as hints while typing;
  • <option value="...">: represents each suggested item.

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

Ask AI

ChatGPT

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

Suggested prompts:

Can you show me a complete example using the `<datalist>` element?

What are some common use cases for `<datalist>` in web forms?

Are there any browser compatibility issues with `<datalist>`?

bookUsing the Datalist Element for Predefined Input Suggestions

Swipe to show menu

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.

index.html

index.html

copy

The <datalist> element is useful when you want to provide helpful suggestions without limiting users to a fixed set of choices.

Example

When the user types in the field, the browser shows the predefined categories. If none match, the user can still enter their own value.

index.html

index.html

copy
  • <label for="category">: describes the purpose of the input field;
  • <input type="text" name="category" id="category" list="categories"/>: a text field that displays suggestions from a linked datalist;
  • <datalist id="categories">: contains predefined options that appear as hints while typing;
  • <option value="...">: represents each suggested item.

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
some-alt