Search code examples
javascriptleaflet

How can I prevent a popup to show when clicking on a marker in Leaflet?


I want a popup that doesn't show itself when I click on the Leaflet marker. I cannot use clickable : false because it will make the markers "act as a part of the underlying map" and this is unacceptable for me. I tried the following code:

marker.on('click', function(event) {
  event.originalEvent.preventDefault();
});

without any results. What is the right way to prevent a popup from showing without using the clickable : false property of the marker object?

All I need is to open all the popups on the map by clicking on one custom button, but I don't want the popups to show themselves after I click on a particular marker.


Solution

  • Just don't bind a popup to the marker. Here's a fiddle with 2 markers. One has a popup and the other does not.

    L.marker([51, 0]).bindPopup("this is a popup").addTo(map);
    
    L.marker([51, 1.5]).addTo(map);
    

    EDIT: I've edited the fiddle and think it might be what you are asking. Here's the important part of the code:

    function onClick(event) {
        event.target.closePopup();
    }