ohyeah ohyeah - 1 year ago 573
Javascript Question

How do you add a class to a Leaflet marker?

I'm using custom divIcons for my Leaflet markers. I want to add a border to whatever marker I click on, with some simple CSS:

.selectedMarker {
border: 10px solid gold;

However, the following with jQuery doesn't work:


Then I tried to use Leaflet's own addClass() method. I tried to call use it in the following ways:

L.addClass(marker, 'selectedMarker');
addClass(marker, 'selectedMarker');
DomUtil.addClass(marker, 'selectedMarker');

None of these work. How do I add the selectedMarker class to my marker?

Answer Source

I have done it by adding a class to the marker with

var marker = L.marker(loc);
marker.on('click', function() {

and then use the css

  //your css
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download