I am using jquery-confirm (https://craftpip.github.io/jquery-confirm/) to confirm a cancellation on a form, but it is not working. My function is not returning the result of the jquery confirmation:
function cnf(cntn, fn, prm) {
var res = false;
$.confirm({
title: "",
content: cntn,
confirmButton: "Yes",
cancelButton: "No",
confirm: function() { res = true; fn(prm); },
cancel: function() { }
});
return res;
}
function emptyfn() { }
function link() {
return cnf("Cancel changes?", emptyfn);
}
...
<form method="post" action="whatever.php">
<label for="test">Test Input</label>
<input type="text" class="form-control" id="test" maxlength="40" name="test" autofocus>
<input type="submit" name="button" value="Submit" class="btn-primary">
<input type="submit" name="button" value="Cancel" class="btn-primary" onclick="return link();" formnovalidate>
</form>
(I know I don't need "fn" and "prm" in this example, but I am trying to generalise the code for use elsewhere).
When I tried to debug the code by adding an alert, things went strange and the alert appeared before the confirmation:
function link() {
var tmp = cnf("Cancel changes?", emptyfn);
alert(tmp);
return tmp;
}
How can I correct the code, and why does my debugging code get executed out of order (is it to do with the return value in the function) ?