33

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:

$(marker).addClass('selectedMarker');

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

marker.addClass('selectedMarker');
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?

3
  • what is the $(marker)?
    – Alien
    Commented Dec 3, 2014 at 9:03
  • jQuery. Which doesn't work, as jQuery targets DOM elements, not JavaScript objects.
    – yesman
    Commented Dec 3, 2014 at 9:04
  • Note you've linked to leaflet docs for L.DomUtil.addClass() but your question has L.addClass().
    – Greg K
    Commented Oct 23, 2020 at 15:57

4 Answers 4

30

In 1.0 and 0.7 you can use L.DomUtil to add remove classes from a DOM element:

L.DomUtil.addClass(marker._icon, 'className');
L.DomUtil.removeClass(marker._icon, 'className');
3
  • 2
    It's worth mentionting that markers must be added to map. Otherwise _icon is undefined.
    – chudy91
    Commented Apr 9, 2020 at 20:57
  • If using L.circleMarker you need to pass marker._path instead of marker._icon
    – Greg K
    Commented Oct 23, 2020 at 16:21
  • is it possible to save the Dom elements when removing the Layer?
    – Odinh
    Commented Jun 17, 2021 at 15:57
22

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

var marker = L.marker(loc);
marker.on('click', function() {
    $(marker._icon).addClass('selectedMarker');
}

and then use the css

.leaflet-marker-icon.selectedMarker{
  //your css
}
0
20

without using jQuery,

marker._icon.classList.add("className");
3
  • 1
    Confirmed this works, thanks. This is the best method to do it I think. Unfortunately classList remains undefined until the user clicks it, so can't add a class right after creating it with this method. Also, tip for those using leaflet markercluster, it's in event.layer._icon.
    – davidtgq
    Commented Mar 4, 2017 at 3:44
  • I had to do this with polygons. layer._path.classList.add("className") worked for me in that case. Commented Jan 15, 2019 at 0:13
  • Use marker._path for L.circleMarker too. Within a click handler you can obtain reference to this from event.target._path
    – Greg K
    Commented Oct 23, 2020 at 16:23
0

I am using the marker = new Marker() syntax, and then marker._icon doesn't work. What solved this for me was to just set a new icon initialized with another class, like so

const originalIcon = new Icon({
    iconUrl: iconImg,
    iconSize: [50, 50],
});
const iconWithNewClass = new Icon({
    iconUrl: iconImg,
    iconSize: [50, 50],
    className: 'new-class',
});

...

marker.setIcon(iconWithNewClass);

Not the answer you're looking for? Browse other questions tagged or ask your own question.