I have a button that I am appending to the page. When that button is clicked, it should run some code.
Instead of triggering only when that button is clicked, any clicks on the page triggers it.
I've been able to replicate the issue with only a few lines of code, see the JSFiddle linked.
var myButton = '<div id="myButton">Do Not Click Me</div>';
$('#parent').html(myButton);
jQuery(document).on('click', jQuery('#myButton'), function (e) {
alert('marco');
});
https://jsfiddle.net/aj4u1y9n/
How can I make the alert run only when the button is clicked?
.on('click', '#myButton', function (e) {...jQueryand$?