Populate HTML is this not created on this line though? – Adam Jan 21 '18 at 00:46

  • That is inside a function, and only executed when the function is called. – connexo Jan 21 '18 at 00:48
  • This code has many issues. Arguments are passed and not used, `parent` is created in a function that is only called after referencing it, but the function also destroys any children added to the parent... the list is too long. I would (honestly!) suggest you start from scratch. – trincot Jan 21 '18 at 00:48
  • Possible duplicate of [use a javascript array to fill up a drop down select box](https://stackoverflow.com/questions/11255219/use-a-javascript-array-to-fill-up-a-drop-down-select-box) – Heretic Monkey Jan 21 '18 at 00:49
  • I am assuming that your script is in the head of your html file, so whenever the code of the definition of `parent` and the subsequent for loop is processed, the ` – rolfv1 Jan 21 '18 at 00:51
  • I'm new to javascript so I'm not totally clued up but if anybody could help me adjust the code accordingly it would be much appreciated so I can learn from my mistakes. – Adam Jan 21 '18 at 00:51
  • Also should be noted my HTML is empty, and doesn't have any ID due to me wanting to do it through javascript – Adam Jan 21 '18 at 00:52
  • @Adam added leaflet tag. You'll get the answer more suited to your problem – zer00ne Jan 21 '18 at 01:00
  • 1 Answers1

    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.

    • Having trouble implementing this into my code, i've replaced my code but fields still show empty. – Adam Jan 21 '18 at 00:57
    • Can you please use "P" instead of "parent" and maybe "C" instead of "child" for variables/ID? – user4723924 Jan 21 '18 at 00:59
    • I have done, however I'm a bit lost it's still not working. – Adam Jan 21 '18 at 01:02
    • Try this... it's difficult to help you from a distance. :) – user4723924 Jan 21 '18 at 01:16
    • https://jsfiddle.net/uteey91y/4/ This is updated using your code, as you can see no info dive box appears in the top. – Adam Jan 21 '18 at 01:24
    • Changed it again... try and follow the logic so you can fix it up your end because I don't have the whole picture from here. – user4723924 Jan 21 '18 at 01:31
    • I want a selection box above the hover over state box however the whole info div has disappeared now. compared to how it shouldlook here https://jsfiddle.net/e1syoc68/ – Adam Jan 21 '18 at 01:36
    • What's wrong with the names `parent` and `child`? They're not reserved, and certainly more readable than `P` and `C`. `S` isn't too friendly, either. – AuxTaco Jan 21 '18 at 04:24
    • Never concatenate arbitrary data into the context of HTML without properly escaping it. Better to set things by properties. – Brad Jan 22 '18 at 00:38