I have a list of buttons to make changes to a forum (each button containing an unique data-mod attribute value). I wrote a jquery script to handle the last clicked button and send its value to a php database handler via ajax, which also sends a custom alert confirmation whether the moderator really wants to do the action or not (if not, alert closes and nothing happens, else, callback function is called).
However, I'm experiencing a weird behavior with this script, which regards the fact that even though I cancel the confirmation, the next confirmed alert will fire the callback function depending on how many times I've clicked the buttons. What I want is the callback function being fired one time with the last confirmed action.
$(function() {
var btn;
// Custom callback function
function callback(clicked) {
$("#result").append("<br/>").append(clicked);
}
// Custom alert function
var alert = {
Check: function(fnc, clicked) {
$("#alert").show();
$("[data-alert=1]").click(function() {
fnc(clicked);
alert.Close();
});
$("[data-alert=0]").click(function() {
alert.Close();
});
},
Close: function() {
$("#alert").hide();
return false;
}
};
// Custom button function
$("[data-mod]").click(function() {
btn = $(this);
return alert.Check(callback, btn.data("mod"));
})
});
If description of the error wasn't clear enough, I've wrote a simplified fiddle so you can make changes at the code: here.
Also, if the error seems a little bit confusing, here's a picture of the reproduction: I want only the last clicked button with the "Yes" confirmation button pressed
Thanks in advance!