[GIS] Using Navbar Dropdown Button Checkbox in Mapbox

bootstrap-frameworkmapbox

I am creating a map application in the Bootstrap framework. I have a drop-down with 3 buttons to toggle layers on and off. I am using a glyphicon as the checkbox with text to the side. If you click the checkbox, the layer switches as expected. However, if you click on the text it changes the checkbox, but does not control the layer toggle. I have tried wrapping the label with a span but this did not work.

Any suggestions?

HTML:

<li class="dropdown"
<a class="dropdown-toggle" data-toggle="dropdown" href="#">BASEMAP <span class="caret"></span></a>
<ul class="dropdown-menu">
<div class="btn-group-vertical btn-block" data-toggle="buttons">
<li><label class="btn btn-default btn-sm btn-block">
<input type="checkbox" class="glyphicon glyphicon-unchecked" id="mapLabel" value="satellite">SATELLITE</label></li>
<input type="checkbox" class="glyphicon glyphicon-unchecked" id="mapLabel" value="dark">DARK</label></li>
</div>
</ul>
</li>

JavaScript:

$("#mapLabel").click(function(event) {
console.log("Click");
    layerClicked = window[event.target.value];

        if (map.hasLayer(layerClicked)) {
            map.removeLayer(layerClicked);
        }
        else{
            map.addLayer(layerClicked);
        } ;
});

Best Answer

Try this jquery method: http://api.jquery.com/find/

$(".your-span-class").find("#mapLabel").click(function(event) { ...

I think the issue is your either selecting a parent element or a child element, which will be easier debugging in a web browser inspector. I don't believe Bootstrap is your issue.

On a separate/quasi-related note, I've found that the Semantic-UI Framework documentation a bit more helpful than Bootstraps with implementing such functionality. And, it works with jQuery. I really don't use Bootstrap anymore, and would recommend Semantic-UI as an alternative to anyone.