Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a HTML5 datalist on the fly

I'm trying to create a datalist on they fly and attach it to an existing input element. But nothing happens (no dropdown arrow is shown) jQuery would be acceptable, too.

var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

function fillDataList() {
    var container = document.getElementById('my-text-box'),
    i = 0,
    len = optionList.length,
    dl = document.createElement('datalist');

    dl.id = 'dlCities';
    for (; i < len; i += 1) {
        var option = document.createElement('option');
        option.value = optionList[i];
        dl.appendChild(option);
    }
    container.appendChild(dl);
}

Fiddle: https://jsfiddle.net/tomsx/704cxako/

(Example taken from this site: http://blogs.microsoft.co.il/gilf/2012/07/30/quick-tip-autocomplete-using-html5-datalist-element/ )

like image 683
smolo Avatar asked Sep 19 '18 03:09

smolo


2 Answers

You have to set the input element's list property as the id of the datalist:

<input id="my-text-box" list="dlCities"/>

Working Code Example:

var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

function fillDataList() {
    var container = document.getElementById('my-text-box'),
    i = 0,
    len = optionList.length,
    dl = document.createElement('datalist');

    dl.id = 'dlCities';
    for (; i < len; i += 1) {
        var option = document.createElement('option');
        option.value = optionList[i];
        dl.appendChild(option);
    }
    container.appendChild(dl);
}
fillDataList();
<input id="my-text-box" list="dlCities"/>

OR: If you do not want to modify the HTML, you can use Element.setAttribute() to set the attribute in JavaScript:

container.setAttribute('list','dlCities');

var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

function fillDataList() {
    var container = document.getElementById('my-text-box'),
    i = 0,
    len = optionList.length,
    dl = document.createElement('datalist');
    container.setAttribute('list','dlCities'); // Set the attribute here

    dl.id = 'dlCities';
    for (; i < len; i += 1) {
        var option = document.createElement('option');
        option.value = optionList[i];
        dl.appendChild(option);
    }
    container.appendChild(dl);
}
fillDataList();
<input id="my-text-box"/>
like image 102
Mamun Avatar answered Oct 15 '22 17:10

Mamun


With only the input present when the page is loaded :

notice how I use "input.setAttribute('list','datalist')" and not "input.list = 'datalist' " directly.

var datalist = document.createElement('datalist');
datalist.id = "datalist";
document.body.appendChild(datalist);
["Seattle", "Las Vegas", "New York", "Salt lake City"].forEach(function(data) {
  var option = document.createElement('option')
  option.value = data
  datalist.appendChild(option)
});
document.querySelector('#my-text-box').setAttribute('list', "datalist");
<input id='my-text-box' />
like image 34
mpm Avatar answered Oct 15 '22 16:10

mpm