0

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);
16
  • 1
    var parent = document.getElementById("parent"); is only evaluated once when JS executes this line. Later, in info.update(), you replace this element with a new element (even though that has the same id, it's a different element). Commented Jan 21, 2018 at 0:31
  • Would it be possible to suggest a fix, as you have pointed out I can still not see where I am going wrong. Commented Jan 21, 2018 at 0:34
  • Just delete the line var parent = document.getElementById("parent"); and you're good to go. This restores the default browser behaviour of making id'd elements accessible by their id without having to declare a variable and using document.getElementById. Commented Jan 21, 2018 at 0:34
  • I have deleted var parent = document.getElementById("parent"); and parent.appendChild(child); Commented Jan 21, 2018 at 0:36
  • However my problem is now that the selection box appears empty instead of displaying what is in the array Commented Jan 21, 2018 at 0:36

1 Answer 1

0

Use this method...

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;
};


var S='';


for(var i=0; i<police_api_dates.length; i++){

  S=S+'<option value="'+i+'">'+police_api_dates[i]+'</option>';

}



info.update = function (properties) {

 info.innerHTML='<select id="parent">'+S+'</select>'+'<h4>Highlighted Postcode</h4>' + (properties ? '<b> Postcode: ' + properties.Name : 'Hover over a state');
};


parent.innerHTML=S; // Insert the options

parent.selectedIndex=3; // Set your default value


info.addTo(mymap);

NB: I would avoid using variable/ID names like "child" and "parent" because you're setting yourself up for javascript reserved word conflicts.

Updated code to get your sequence in order... let's see how that goes.

Sign up to request clarification or add additional context in comments.

9 Comments

Having trouble implementing this into my code, i've replaced my code but fields still show empty.
Can you please use "P" instead of "parent" and maybe "C" instead of "child" for variables/ID?
I have done, however I'm a bit lost it's still not working.
Try this... it's difficult to help you from a distance. :)
jsfiddle.net/uteey91y/4 This is updated using your code, as you can see no info dive box appears in the top.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.