I'm attempting to populate a HTML selection box from a JavaScript array, however when I run my page my console is outputting the following:
Uncaught TypeError: Cannot read property 'appendChild' of null at hello.html:113
However, because it is created by JavaScript innerHTML
I cannot select it. Is there another way of selecting this?
var police_api_dates = ["&date=2017-03",];
var info = L.control();
info.onAdd = function (mymap) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (properties) {
this._div.innerHTML = '<select id="parent"></select><h4>Highlighted Postcode</h4>' + (properties ?
'<b> Postcode: ' + properties.Name
: 'Hover over a state');
};
var parent = document.getElementById("parent");
for ( var pos = 0; pos < police_api_dates.length; pos++)
{
//create an <option> to add the <select>
var child = document.createElement("option");
//assign values to the <option>
child.textContent = police_api_dates[pos]
child.value = pos;
//attach the mew <option> to the <selection>
parent.appendChild(child);
}
info.addTo(mymap);