I am creating popups in a Leaflet map. The popup content should be created dynamically using javascript. On adding content to a div-container inside the popup with innerHTML, I get the error message: "Cannot set property 'innerHTML' of null"
Here is my code:
//create div for form element "Baumpatenschaft"
var div_baumpatenschaft = document.createElement('div');
//add div_baumpatenschaft to DOM (child of <div> with id="formular")
formular.appendChild(div_baumpatenschaft);
//create ID for div
div_baumpatenschaft.id = "div_baumpatenschaft";
var pt = document.getElementById("div_baumpatenschaft");
//HTML code for creating radio buttons stored in var baumpatenschaft
var baumpatenschaft =
'<label for="radio_ja">ja</label><br/>\
<input checked="checked" type="radio" name="baumpatenschaft" id="radio_ja" value="ja"/>\
<label for="radio_nein">nein</label><br/>\
<input type="radio" name="baumpatenschaft" id="radio_nein" value="nein"/>';
//adding the HTML-code to div_baumpatenschaft
pt.innerHTML = baumpatenschaft;
Apparently the div_baumpatenschaft isn't created yet, as innerHTML tries to insert the HTML-code inside the <div>.
I have also tried element.insertAdjacentHTML() with the same error.
What am I doing wrong?
Is there another possibility to get around that issue? I definitely want to create the div with document.createElement('div') and I also want to define the radio buttons as HTML-text stored in a variable and not with document.createElement('input'), what I have done before.
div_baumpatenschaftfor the.innerHTMLpart and add it to the DOM with.appendChild()