So I want to add some HTML code to a Leaflet popup, but I also want to be able to to perform some jQuery functions when this HTML is clicked, like so:
.bindPopup( "<button id='clickHere'> <strong> stuff </strong> </button>" ))
But I'm unable to access this using jQuery selectors, because it's inside tags.
Is there any way to solve this problem?
1 Answer 1
If you want to perform a simple action, you can just use the onclick
attribute, like this:
.bindPopup( "<button id='clickHere' onclick='alert(...)'> <strong> stuff </strong> </button>" ))
An other, slightly more difficult way to do this would be to catch the popupopen
event and bind an action to the onclick
event inside it:
map.on('popupopen', function(e) {
var marker = e.popup._source; # Source popup
$('#clickHere').click(function() {
...
});
});
-
Great thanks, that is perfect. One more quick question, It's possible to make onclick calls to an external JS file, right?Strobe_– Strobe_2016年04月11日 11:32:07 +00:00Commented Apr 11, 2016 at 11:32
-
1If the JS file is correctly loaded with the HTML file, yes, absolutely. You should however ensure that your external file is loaded after the main document (this often leads to trouble).ArMoraer– ArMoraer2016年04月11日 11:44:50 +00:00Commented Apr 11, 2016 at 11:44