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.
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>
Try typing P, J, or another value. The exact presentation of suggestions is controlled by the browser.
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.
Each <option> supplies a suggestion through its value.
<datalist id="browsers">
<option value="Chrome"></option>
<option value="Firefox"></option>
<option value="Safari"></option>
</datalist>
<option> is not a void element, so prefer <option value="PHP"></option> rather than self-closing markup such as <option value="PHP" />.| Datalist | Select |
|---|---|
| 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)
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.
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.
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.
Modern browsers generally support datalist, but the suggestion interface, filtering behavior and keyboard interaction can vary.
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
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.
Datalist provides suggestions. Users can normally type another value.
The input's list value must match the datalist's id.
The input is the form control that is submitted. Give the input the name.
Use normal <option></option> markup because option is not a void HTML element.
Required only requires a value. It does not restrict that value to the datalist options.
The browser controls much of the suggestion interface, so presentation and interaction can vary.
It provides a set of suggested values for an associated input element.
Yes. Datalist suggestions do not normally restrict the input to only those values.
Set the input's list attribute to the id of the datalist.
Datalist suggests values while still allowing custom input. Select represents a defined set of options.
No. Required only requires the input to contain a value.
Not reliably. The browser controls much of the native suggestion interface.
Modern Safari supports datalist, but the presentation and interaction can differ between browsers.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| vamsi | 24-11-2014 |
| How can i make to submit the form after selecting a option from datalist ?? | |