Search code examples
javascriptc#jquerypromptalertify

Alertify does not stop for prompt


So I have these lines of code:

$('td').click(function (e) {
    var test = "....";
    var test2 = "....";
    var test3 = "....";
    SuppressWarning(test, test2, test3);
});

function SuppressWarning(test, test2, test3)
{
  var reason = prompt("Why are you suppressing this warning?");
  if(reason != "")
  {
    //do something
  }

When the system runs, it stops at the prompt and does not proceed to the if statement unless the user interacts. However, I want to customise the alert box so I decided to use alertify.js .

So I have now ended up with this code:

$('td').click(function (e) {
var test = "....";
var test2 = "....";
var test3 = "....";
SuppressWarning(test, test2, test3);
});

function SuppressWarning(test, test2, test3)
{
var reason = alertify.prompt("Message", function (e, str) {
if (e) {
    // user clicked "ok"
} else {
    // user clicked "cancel"
}
}, "Default Value");

if(reason != "")
  {
    //do something
  }

My issue is that when the system runs, it does not stop at the prompt; It just goes to the if statement without and does the rest. Why is that? Why does it not stop at the prompt so that the user can type in something? How can I fix this? Thank you!


Solution

  • prompt is native to the browser and stop the current flow of the code, just like an alert box or confirm. On the other hand, alertify doesn't. The script will not stop if alertify is triggered.

    That's why alertify accept a callback as argument. The function(e, str) is triggered after the user press ok or cancel. The str argument is what the user inputed. Just move your condition inside that callback:

    alertify.prompt("Message", function (e, str) {
        if (e) {
            // user clicked "ok"
            if(str != "")
            {
                //do something
            }
        } else {
            // user clicked "cancel"
        }
    }, "Default Value");