Skip to main content

HTML <datalist> Tag

The HTML <datalist> element is a list of pre-defined options for an <input> element. It allows you to provide a list of suggestions for the user as they type, but does not limit their choices to just the options in the list.

To use the <datalist> element, you first need to create an <input> element with a unique id attribute. Then, you can create a <datalist> element with a list attribute that has the same value as the id attribute of the <input> element. Inside the <datalist> element, you can then define the options for the user by using <option> elements.

Here's an example of how to use the <datalist> element:

<label for="favorite-fruit">Favorite Fruit:</label>
<input list="fruits" id="favorite-fruit" name="favorite-fruit">
<datalist id="fruits">
<option value="Apple">
<option value="Raspberry">
<option value="Orange">
<option value="Mango">
<option value="Pineapple">
<option value="Peach">
<option value="Plum">
<option value="Blueberry">
</datalist>

In this example, the <input> element has a list attribute with a value of "fruits", which refers to the id attribute of the <datalist> element. The <datalist> element then contains a list of <option> elements, each with a value attribute that specifies the text that will be displayed in the dropdown list of options.

When the user starts typing in the <input> element, they will see a list of suggestions based on the options in the <datalist> element. They can choose one of these options, or they can continue typing to enter a different value.

The <datalist> element is a useful tool for providing suggestions to users as they enter data into a form, and can help improve the usability and user experience of your web application.

I hope this helps to give you a better understanding of the HTML <datalist> element and how it can be used in your web development projects.