jquery - Dynamic div displaying incorrectly -
i have dynamic legend being added leaflet map.
my issue div.innerhtml +='</div>'
gets called before if statements , statement have finished.
i've been looking callbacks i'm stuck after hours of looking @ this, , seem going in circles
/*legend control */ function maplegendcontrol() { dynamicmaplegend = l.control({ position: 'topright' }); dynamicmaplegend.onadd = function(map) { var div = l.domutil.create('div', 'legend'), legendcolors = ["#7c7c7c", "#9d2a2a", "#26a82f"], legendlabels = ["blue", "red", "green"]; div.innerhtml += '<div class="legend-inner-top">' + '<div class="legend-inner-top-m" style="padding-top: 3px color: #e3e3e3;"><strong style="color: #e3e3e3">legend</strong></div>' + '<div class="legend-inner-top-m" style="float: right;"><button type="button" class="btn btn-xs btn-legend closelegend"></button></div>' + '</div>' + '<div class="legend-inner-bottom">' if (map.haslayer(jungle)) { // loop through our density intervals , generate label colored square each interval (var = 0; < legendcolors.length; i++) { div.innerhtml += '<div style="margin-top: 10px;"><i style="background:' + legendcolors[i] + '"></i>' + legendlabels[i] + "</div>"; } } if (map.haslayer(tropical)) { div.innerhtml += '<i><img src="assets/img/legend-tropical.png" alt="tropical icon" height="18" width="18"></i>' + 'tropical' + "<div style='margin-top: 10px;'></div>"; } if (map.haslayer(forest)) { div.innerhtml += '<i><img src="assets/img/legend-forest.png" alt="forest icon" height="18" width="18"></i>' + 'forest' + "<div style='margin-top: 10px;'></div>"; } div.innerhtml += '</div>' return div; } dynamicmaplegend.addto(map); }
one problem here .innerhtml
doesn't work way you're expecting. it's not document.write()
can write things html page piecemeal, <div>
, </div>
, things in between written out separately.
this may why appears </div>
being added early: if assign .innerhtml
, don't close tags, browser you. first .innerhtml
assignment near top of onadd()
doesn't leave tags open document.write()
do, closes them right there. that's why other tags written below aren't getting nested inside.
if use .innerhtml
should create string complete dom fragment in it, , after string built, assign entire string .innerhtml
.
starting code have now, direct translation be:
/*legend control */ function maplegendcontrol() { dynamicmaplegend = l.control({ position: 'topright' }); dynamicmaplegend.onadd = function(map) { var div = l.domutil.create('div', 'legend'), legendcolors = ["#7c7c7c", "#9d2a2a", "#26a82f"], legendlabels = ["blue", "red", "green"], html = ''; html += '<div class="legend-inner-top">' + '<div class="legend-inner-top-m" style="padding-top: 3px color: #e3e3e3;"><strong style="color: #e3e3e3">legend</strong></div>' + '<div class="legend-inner-top-m" style="float: right;"><button type="button" class="btn btn-xs btn-legend closelegend"></button></div>' + '</div>' + '<div class="legend-inner-bottom">' if (map.haslayer(jungle)) { // loop through our density intervals , generate label colored square each interval (var = 0; < legendcolors.length; i++) { html += '<div style="margin-top: 10px;"><i style="background:' + legendcolors[i] + '"></i>' + legendlabels[i] + "</div>"; } } if (map.haslayer(tropical)) { html += '<i><img src="assets/img/legend-tropical.png" alt="tropical icon" height="18" width="18"></i>' + 'tropical' + "<div style='margin-top: 10px;'></div>"; } if (map.haslayer(forest)) { html += '<i><img src="assets/img/legend-forest.png" alt="forest icon" height="18" width="18"></i>' + 'forest' + "<div style='margin-top: 10px;'></div>"; } html += '</div>'; div.innerhtml = html; return div; } dynamicmaplegend.addto(map); }
Comments
Post a Comment