1

I am writing a popup on map pin click, where the user clicks on a link which initiates a function call. Here's the call that creates the pin

new mapboxgl.Marker(el)
            .setLngLat(marker.geometry.coordinates)
            .setPopup(new mapboxgl.Popup({offset: 25}) // add popups
                .setHTML(popup(marker.properties)))
            .addTo(map);

And here's the popup function

function popup(properties) {
    return '<div>' +
        '<h3>' + properties.partnerName + '</h3>' +
        '<a onclick="mapPinSelect()">' + 'Link' + '</a>' +
        '</div>';
}

Everything works fine, but when I convert my popup method to JQuery, my onClick method stops working. Here's the JQuery version

function popup(properties) {
    return $('<div>')
            .append($('<h3>').append(properties.partnerName))
            .append($('<a>').click(mapPinSelect).append('Link'))
            .html()
}

What am I doing wrong?

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59

1 Answers1

0

You should use "on", that should work for dynamically created elements.

$("div#container").on('click', 'button.alert', function() {
    alert(1);
});

https://api.jquery.com/on/ Difference between .on('click') vs .click()

user2345998
  • 649
  • 13
  • 31