Friday, 3 March 2017

leaflet - How to update a popup content from its marker?



I created a popup with Leaflet doing so:


marker.bindPopup(content).openPopup();

How can I update the content value afterwards?


I suppose to do it from the marker, something like this:


marker.updatePopup(newContent);

Answer



I suppose you want the content to be changed after some event occured, like mouseover, contextmenu, or anything else.


To do so, you can use the following code:


//marker creation

var marker = L.marker([44.63, 22.65]).bindPopup('something').addTo(map);
marker.openPopup();

//changing the content on mouseover
marker.on('mouseover', function(){
marker._popup.setContent('something else')
});

As you can see, you can acccess the popup for the desired marker using marker._popup method, and then use setContent method to change the text inside it.


popup.setContent method reference



Here's some code on Plunker demonstrating this: http://plnkr.co/edit/vjS495QPXiJpKalrNpvo?p=preview


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...