javascript - Leaflet toggle buttons only work on the first click -
i have button want use toggle map layer on , off. click once , layer adds map, click again , goes away (and on). right button works on first click , none thereafter.
i think has fact it's written if/else statement. condition satisfied, function ends, , that's end of it.
looking guidance... how modify code make functional toggle button?
my button:
<button id="buttona" onclick=="onoff();">button a</button>
then, within function $.getjson("mydata.json", function(data) {...});
have following:
function onoff(){ currentvalue = document.getelementbyid('onoff').value; } document.getelementbyid("buttona").addeventlistener("click", function(){ if (currentvalue = "off"){ layera.addto(map); document.getelementbyid("onoff").value="on"; } else{ map.removelayer(layera); document.getelementbyid("onoff").value="off"; } });
change if (currentvalue = "off")
if (currentvalue == "off")
edited: try in way:
button:
<button id="buttona" onclick="onoff();">button a</button>
and js:
function onoff(){ currentvalue = document.getelementbyid('onoff').value; if (currentvalue == "off"){ layera.addto(map); document.getelementbyid("onoff").value="on"; } else{ map.removelayer(layera); document.getelementbyid("onoff").value="off"; } }
Comments
Post a Comment