HTML Datalist: Suggested Values for an Input

The HTML <datalist> element provides suggested values for an <input>. The user can choose one of the suggestions or type a different value.

<label for="section">Section</label>
<input type="text" id="section" name="section" list="scripts">

<datalist id="scripts">
  <option value="PHP"></option>
  <option value="JavaScript"></option>
  <option value="ASP"></option>
  <option value="Dot Net"></option>
  <option value="Perl"></option>
  <option value="JSP"></option>
</datalist>

The input's list value must match the datalist's id. The datalist itself is not the submitted form control; the associated input supplies the submitted name and value.

Datalist provides suggestions, not a closed list. If users must choose only from approved values, use a select element or validate the input against an allowlist in the receiving application.

How Datalist Works Top ↑

A datalist contains predefined suggestions. An input is linked to it through the list attribute.

<input type="text" name="town" list="towns">

<datalist id="towns">
  <option value="Hyderabad"></option>
  <option value="Bhubaneswar"></option>
  <option value="Visakhapatnam"></option>
</datalist>
  • The browser can show matching suggestions as the user types.
  • The user can select a suggestion.
  • The user can also enter a different value.
  • The selected or typed value belongs to the input, not to the datalist.

Live Datalist Demo Top ↑

Try typing P, J, or another value. The exact presentation of suggestions is controlled by the browser.

input list and datalist id Top ↑

The connection between the two elements is created by matching these values:

<input list="languages" name="language">

<datalist id="languages">
  <option value="Python"></option>
  <option value="PHP"></option>
</datalist>

If the values do not match, the input will not use that datalist.

Datalist Options Top ↑

Each <option> supplies a suggestion through its value.

<datalist id="browsers">
  <option value="Chrome"></option>
  <option value="Firefox"></option>
  <option value="Safari"></option>
</datalist>
Use normal HTML option syntax. <option> is not a void element, so prefer <option value="PHP"></option> rather than self-closing markup such as <option value="PHP" />.

Datalist vs Select Top ↑

DatalistSelect
Provides suggested values for an input.Provides a defined set of selectable options.
The user can normally type a value not present in the list.The user normally chooses from the supplied options.
Useful for guided but open-ended input.Useful when the choice must come from a controlled list.
Suggestion popup appearance varies by browser.Select presentation also varies, but the control represents a closed set.
The submitted value belongs to the input.The submitted value comes from the selected option.

More about Dropdown Listbox (SELECT)

When to Use Datalist Top ↑

HTML datalist suggestions while typing

Datalist is a good choice when you have useful suggestions but still want to allow another value.

For example, you can suggest common town names without assuming your list contains every possible town.

Use <select> instead when the user must choose one of the supplied values.

Validation and Allowed Values Top ↑

Datalist suggestions do not restrict the value to the listed options.

<input type="text" name="language" list="languages2">

<datalist id="languages2">
  <option value="Python"></option>
  <option value="PHP"></option>
</datalist>

The user can still submit Java, Ruby, or another value.

Do not treat a datalist as validation. If only known values are allowed, the receiving application must validate the submitted input against an allowlist or use a select control.

required with Datalist Top ↑

The required attribute can require the associated input to contain a value:

<label for="city">City</label>
<input type="text" id="city" name="city" list="cities" required>

<datalist id="cities">
  <option value="Hyderabad"></option>
  <option value="Bengaluru"></option>
</datalist>

required means the field cannot be empty for browser constraint validation. It does not mean the value must be one of the datalist options.

Browser and Accessibility Behavior Top ↑

Modern browsers generally support datalist, but the suggestion interface, filtering behavior and keyboard interaction can vary.

  • The browser decides how the suggestion popup looks.
  • Matching behavior can differ between browser engines.
  • Mobile browsers may present suggestions differently from desktop browsers.
  • Assistive-technology behavior can vary, so important form instructions should remain visible outside the popup.
The old statement that Safari does not support datalist is outdated. Modern Safari supports datalist, although presentation and interaction can still differ from other browsers.

Styling Datalist Suggestions Top ↑

You can style the associated <input> like other form controls, but the browser-generated suggestion popup offers limited styling control.

<input
  type="text"
  name="section"
  list="scripts3"
  class="form-control"
>

If a project requires a fully styled autocomplete popup, a JavaScript autocomplete component may provide more control.

Displaying matching words using jQuery UI Autocomplete

Dynamic Datalist Options Top ↑

Datalist options can be created or replaced with JavaScript when suggestions change in the browser.

<input type="text" id="skill" name="skill" list="skills">
<datalist id="skills"></datalist>

<script>
const values = ['HTML', 'CSS', 'JavaScript'];
const list = document.getElementById('skills');

values.forEach(value => {
  const option = document.createElement('option');
  option.value = value;
  list.appendChild(option);
});
</script>

For a dedicated example, see adding datalist options with JavaScript.

Plus2net Datalist Applications Top ↑

Video Tutorial Top ↑

HTML Datalist for selection of options or adding new input by user

Common Datalist Mistakes Top ↑

Expecting datalist to restrict the input Top ↑

Datalist provides suggestions. Users can normally type another value.

Using different list and id values Top ↑

The input's list value must match the datalist's id.

Putting name on the datalist instead of the input Top ↑

The input is the form control that is submitted. Give the input the name.

Using self-closing option syntax Top ↑

Use normal <option></option> markup because option is not a void HTML element.

Assuming required means "must match a suggestion" Top ↑

Required only requires a value. It does not restrict that value to the datalist options.

Depending on identical popup behavior in every browser Top ↑

The browser controls much of the suggestion interface, so presentation and interaction can vary.

Frequently Asked Questions Top ↑

Q1: What does datalist do in HTML?

It provides a set of suggested values for an associated input element.

Q2: Can a user enter a value that is not in the datalist?

Yes. Datalist suggestions do not normally restrict the input to only those values.

Q3: How do I connect an input to a datalist?

Set the input's list attribute to the id of the datalist.

Q4: What is the difference between datalist and select?

Datalist suggests values while still allowing custom input. Select represents a defined set of options.

Q5: Does required force a datalist value to match one of its options?

No. Required only requires the input to contain a value.

Q6: Can I fully style the datalist suggestion popup?

Not reliably. The browser controls much of the native suggestion interface.

Q7: Does Safari support datalist?

Modern Safari supports datalist, but the presentation and interaction can differ between browsers.


HTML Form Dropdown Listbox Checkbox



plus2net.com







vamsi

24-11-2014

How can i make to submit the form after selecting a option from datalist ??



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer