Adding Options to a Datalist with JavaScript

The HTML <datalist> element provides suggestions for an associated input. JavaScript can populate those suggestions from an array, API response, database result returned by the server, or any other data source.

<label for="month">Month</label>
<input id="month" name="month" list="months" autocomplete="off">
<datalist id="months"></datalist>

Populate a Datalist from an Array Top ↑

The original example built a string and assigned it with innerHTML. Creating option elements directly avoids HTML-string construction and is easier to extend.

const months = [
  "January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

const monthList = document.getElementById("months");
for (const month of months) {
  const option = document.createElement("option");
  option.value = month;
  monthList.appendChild(option);
}

Count and Read Options Top ↑

const monthList = document.getElementById("months");
console.log(monthList.options.length); // 12
console.log(monthList.options[5].value); // June

Validate a Submitted Value Top ↑

A datalist suggests values but does not force the visitor to select one of them. If only listed values are valid, check the submitted value explicitly.

<form id="fruit-form">
  <label for="fruit-input">Fruit</label>
  <input list="fruits" name="fruit" id="fruit-input">
  <datalist id="fruits">
    <option value="Apple"></option>
    <option value="Banana"></option>
    <option value="Orange"></option>
  </datalist>
  <button type="submit">Submit</button>
</form>
<p id="fruit-result" aria-live="polite"></p>
<script>
const allowedFruits = ["Apple", "Banana", "Orange"];
const form = document.getElementById("fruit-form");
const result = document.getElementById("fruit-result");

form.addEventListener("submit", (event) => {
  event.preventDefault();
  const selectedFruit = document.getElementById("fruit-input").value;
  result.textContent = allowedFruits.includes(selectedFruit)
    ? `Valid fruit: ${selectedFruit}`
    : "Choose a fruit from the suggestions.";
});
</script>

Respond to Input Changes Top ↑

const monthInput = document.getElementById("month");
monthInput.addEventListener("change", () => {
  console.log(`Selected or typed: ${monthInput.value}`);
});

Options from a Database Top ↑

The browser does not connect directly to MySQL. A server endpoint can return values, which JavaScript then inserts into the datalist. See populate datalist options using PHP and Ajax.

Questions to Review Top ↑

  1. What does a datalist provide to an input?
  2. How can JavaScript create options dynamically?
  3. How is a datalist associated with an input?
  4. Does a datalist restrict users to listed values?
  5. How can you read the selected or typed value?
  6. Which events are useful for responding to input changes?
  7. How can a server/database supply suggestions?
  8. What should be considered for accessibility and browser support?

JavaScript Validation Validation of dropdown Listbox




Subscribe to our YouTube Channel here



plus2net.com







yraml

04-12-2016

how to get data-id in option like these ?

23-08-2021

how to get data-id in option like these ? i am triyng make input with data-* attribute for insert to my db values from table (users)

17-08-2022

I m trying to populate datalist dynamically with Ajax call. all the options gets populated when i check in source code. But actually UI shows only first option in datalist dropdown. Why is this?



✖
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