I am designing a simple prototype web mapping application where I want a user to be able to click on a dropdown menu and have the map's choropleth layer, choropleth legend, and hover info box all change at once. My code seems to work up until the end, where when selecting a layer from the dropdown, nothing happens on my map. I know something is wrong with my change function. I am very new to Javascript, and have been using Leaflet in my class. Here is some of my code. The var here are both variable objects containing three other variables.
https://codepen.io/rjankows88/pen/zYpJNpW
var evlayer = {legend,
info,
counties
};
var poplayer = {poplegend,
popinfo,
population
};
var myDropdown = L.Control.extend({
options: {
position: "topright"
},
onAdd: function(map) {
this._div = L.DomUtil.create("div", "myDropdown");
this._div.innerHTML =
'<h2 id="title">Select a layer </h2>' +
'<select id="selector">' +
'<option value="init">Any</option>' +
'<option value="evlayer">Electric Vehicles per County</option>' +
'<option value = "poplayer">Population Estimate per County</option>' +
"</select>";
L.DomEvent.disableClickPropagation(this._div);
return this._div;
}
});
map.addControl(new myDropdown());
var layer_select = L.DomUtil.get("selector");
//prevent clicks on the selector from propagating through to the map
//(otherwise popups will close immediately after opening)
L.DomEvent.addListener(layer_select, "click", function(e) {
L.DomEvent.stopPropagation(e);
});
L.DomEvent.addListener(layer_select, "change", change);
function change(e) {
// updating the layer means deleting the old one and
// adding a new one
//debugger;
$("select").change(function (e) {
var x = document.getElementById("select");
if (x == "Electric Vehicles per County") {
map.removeLayer(poplayer);
map.addLayer(evlayer);
} else if (x == "Population Estimate per County") {
map.removeLayer(evlayer);
map.addLayer(poplayer);
}
});
};