I have two triggers, one which returns a boolean
from a confirm
and the other that sends a ajax request
.
I noticed that simply using the functions as they are, even if you click cancel the ajax still follows through. My issue is that the confirm trigger
is broad and is used by more than one trigger used for different things. So how would I go about checking if confirm returns false?
Heres an example:
This is my code to check to see if the confirm link is clicked
$('div.answers').on('click', '.confirm', function() {
return confirm('Are you sure?');
});
This is the code which does ajaxy things but also has the confirm
class:
$('div.answers').on('click', '.trg-delete', function() {
var that = $(this);
var itemid = that.data("id");
$.ajax({
type: "POST",
url: "./delete-answer.php",
data: { itemID: itemid }
})
.done(function(data) {
if(data == 1) {
that
.css("color", "#27AE60")
.html('<i class="fa fa-fw fa-check"></i>');
} else {
that
.css("color", "#C0392B")
.html('<i class="fa fa-fw fa-close"></i>');
};
window.setTimeout(function() {
that
.css("color", "")
.html('<i class="fa fa-fw fa-close"></i>');
}, 5000);
})
.fail(function(data) {
that
.css("color", "#C0392B")
.html('<i class="fa fa-fw fa-close"></i>');
window.setTimeout(function() {
that
.css("color", "")
.html('<i class="fa fa-fw fa-close"></i>');
}, 5000);
});
});
As you can see, these two are completely separate as I also have triggers such as trg-open
, trg-closed
, trg-choice
which all do totally different things, but also have the confirm
class.
What would be the easiest way to edit, in this example, the trg-delete
on click to check to see if the confirm
on click returns true
or false
?
Note: I did see this but it is different as I am using on('click')
s where he has functions.