Adding options to a datalist.

We will prepare a datalist without any option or data added to it.
<input name="month" list="months" >
<datalist id="months" >
</datalist>
We will use one array to store months of a year and then add them as option to the datalist. We will loop through all the elements of the array and prepare the format to store the options in a string variable by using all elements.
var str=''; // variable to store the options
var month = new Array("January","February","March","April","May","June","July","August",
"September","October","November","December"); for (var i=0; i < month.length;++i){ str += '<option value="'+month[i]+'" />'; // Storing options in variable }
Then we will assign this string variable to the datalist by using innnerHTML property.
var my_list=document.getElementById("months");
my_list.innerHTML = str;
Each element of our array will be added as option values of the datalist.

The output is here .



Full code is here
<input name="month" list="months" >
<datalist id="months" >
</datalist>

<script language="javascript">
var str=''; // variable to store the options
var month = new Array("January","February","March","April","May","June","July","August",
"September","October","November","December"); for (var i=0; i < month.length;++i){ str += '<option value="'+month[i]+'" />'; // Storing options in variable } var my_list=document.getElementById("months"); my_list.innerHTML = str; </script>

Number of options available in a Datalist

var my_list=document.getElementById("months");
alert(my_list.options.length);
Output will be 12

Displaying particular option value.

alert(my_list.options[5].value);
Output will be June ( Note : options[0].value is January )

Example: Dynamically Populating datalist with JavaScript

<input list="cities" id="city" placeholder="Type city...">
<datalist id="cities"></datalist>

<script>
var cities = ['New York', 'Los Angeles', 'Chicago', 'Houston'];
var dataList = document.getElementById('cities');

cities.forEach(function(city) {
    var option = document.createElement('option');
    option.value = city;
    dataList.appendChild(option);
});
</script>

Example: Handling Form Submission with datalist

<form onsubmit="handleSubmit(event)">
    <input list="fruits" name="fruit" id="fruitInput">
    <datalist id="fruits">
        <option value="Apple">
        <option value="Banana">
        <option value="Orange">
    </datalist>
    <input type="submit" value="Submit">
</form>

<script>
function handleSubmit(e) {
    e.preventDefault();
    const selectedFruit = document.getElementById('fruitInput').value;
    if (['Apple', 'Banana', 'Orange'].includes(selectedFruit)) {
        alert("Valid fruit: " + selectedFruit);
    } else {
        alert("Invalid input");
    }
}
</script>
Explanation:
Dynamic Datalist: Demonstrates populating a `` dynamically using JavaScript, useful for generating real-time suggestions.
Form Handling: Illustrates how to validate and handle user input through a form submission.

Adding options from MySQL table.

Here we have seen how to add options to a datalist by using client side JavaScript. We can populate options of a datalist by taking data from database table by using PHP and Ajax.

We can use events connected to input element.
<input name="month" list="months" onchange='my_alert()'>
function my_alert()
{
alert('I am changed');
}

Questions


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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer