Skip to content
Advertisement

Leaflet map with embedded Bootstrap Switch toggle

I am trying to add a bootstrap switch inside my leaflet.js map.

So far I have a working button (see snippet) but I want to use a switch instead.

See attached image:

enter image description here

So far it is a complete failure.

Among the things I have tried is the code below (which obviously does not work):

JavaScript

Does anyone know how this should work please? As always, any help is greatly appreciated. If the same toggle switch can be achieved in some other way (ie without bootstrap) that is also going to be fine.

Many thanks!

JavaScript

Advertisement

Answer

  1. The $("[class='mySwitch']") finds Elements based on the string selector. You have to adjust the Bootstrap Switch example to your usage. In your case, you do not need a selector but you can directly pass the HTML Element you created, so that it is wrapped by jQuery and can be transformed by Bootstrap Switch: $(container).bootstrapSwitch({})

  2. Do not try to transform your Control container directly, but embed a child checkbox input into that container:

JavaScript
  1. You have a typo in:
JavaScript

…should be:

JavaScript

Live result:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement