How to show an image and a link on a popup when clicking on a leaflet marker?

Currently, I have this code to show an image on a popup when clicking on the marker:

const marker3 = L.marker([22.78257, -94.5612], {icon: redIcon}).on('click', onClick2).addTo(map);

// When click on red marker, open popup with the image
function onClick2(e) {
    popupContent = document.createElement("img");
    popupContent.src = "";
    marker3.bindPopup(popupContent, {
        maxWidth: "auto"

I want to also show a link to the image on the popup, so that when clicked, it can be displayed full size on a new tab on the browser. Preferable the link is edited or something, so that it does not take much space on the screen.


How about changing the popupContent like below so that you can change the popup content flexibly? (The link should work in your environment.)

Also, I think onClick function is not necessary.

const src = "";
const popupContent = document.createElement("div")
popupContent.innerHTML = "<img src='" + src + "'>"
                       + "<a target='_blank' href='" + src + "'>See the image</a>"

const map ='map').setView([10, 20]);
const marker = L.marker([10, 20]).bindPopup(
    { maxWidth: "auto" }
#map {
    height: 360px;
    <link rel="stylesheet" href="[email protected]/dist/leaflet.css"
        crossorigin="" />
    <script src="[email protected]/dist/leaflet.js"
    <div id="map"></div>

Leave a Reply

Your email address will not be published. Required fields are marked *