I have a popup confirmation plugin that is triggered when you click on it (to make sure the user wants to do a specific action). The plugin is working perfectly on links on the page using this code:
$('.confirmation-callback').confirmation({
onConfirm: function(event, element) {
alert('hello');
}
});
But now the problem is when I create "new" elements via ajax on the page, they are not bound to the code above, and when you click on the new elements nothing happens.
I know the problem is that I need to use the on()
function to ensure all events are captured on dynamically generated elements. I have read answers such as here and here that give various versions of it.
I've tried a few things to change the code to work for on()
- but I cant seem to get it to work.
I tried just wrapping the whole thing in an on()
call:
$(document).on("click",".confirmation-callback",function(){
alert('i see this message'); <<---- I see this on the new elements
$('.confirmation-callback').confirmation({
onConfirm: function(event, element) {
alert('but this alert never shows');
}
});
});
But although I see the first alert, the rest of the confirmation()
function is never called.
So how do I force the confirmation()
function to run when on()
is run?