Naib Sorion
Naib Sorion

Reputation: 504

jQuery confirm.js return result to boolean

I'm using this plugin, I wrapped a confirm method into function so I can use it every-time https://jsfiddle.net/8g0j4unj/

function askQuestion($msg){
    $.confirm({
        icon : 'fa fa-question-circle-o',
        content : ''+$msg+'',
        theme : 'supervan',
        closeIcon: true,
        animation: 'scale',
        type: 'orange',
        draggable:'true',
        buttons:{
            'Continue' : {
                keys : ['enter'],
                action : function(){
                    return 1;
                }
            },
            'Cancel': {
                keys : ['esc'],
                action: function(){
                    this.close();
                }
            }
        }
    });
}

When I try to this function it doesn't do any action but continuing the process, how can I return the function to boolean when user confirm or cancel the condition?

$(document).on('click','.sample',function(){
    if(askQuestion('Are you sure want to continue without adding a server') == 1){
                alert('Confirmed');
            }
    else{
       alert('Cancelled');
    }

});

Upvotes: 1

Views: 1250

Answers (2)

Arun Tom
Arun Tom

Reputation: 879

This can be done with callback function.

$(document).on('click','.sample',function(){
askQuestion('Are you sure want to continue without adding a server', function(val){               
           if(val==1){
              alert("Msg: confirmed");
              //do something
           }
           else{
              alert("Msg : rejected");
           }
  });
});

function askQuestion($msg, callback){
$.confirm({
    icon : 'fa fa-question-circle-o',
    content : ''+$msg+'',
    theme : 'modern',
    closeIcon: true,
    animation: 'scale',
    type: 'orange',
    draggable:'true',
    buttons:{
        'Continue' : {
            keys : ['enter'],
            action : function(){
                callback(1);
            }
        },
        'Cancel': {
            keys : ['esc'],
            action: function(){
                this.close();
                callback(0);

            }
        }
    }
});
}

This is how callback is used,

So instead of using askQuestion() inside if() statement, write your code and conditions inside the callback function body.

JS Fiddle

Upvotes: 1

Pedram
Pedram

Reputation: 16575

I really have no idea why you want to do this while this plugin have callback function and you can use it easily. You trying to detect if user click Confirm or Cancel button, so you don't need to use this:

$(document).on('click','.sample',function(){});

And your code totally wrong because you want to get returned data from a function on click on a button! but with this?

askQuestion('Are you sure want to continue without adding a server') 

Actually it return nothing. anyway. You can detect simply with callback

function askQuestion($msg){
    $.confirm({
        icon : 'fa fa-question-circle-o',
        content : ''+$msg+'',
        theme : 'supervan',
        closeIcon: true,
        animation: 'scale',
        type: 'orange',
        draggable:'true',
        buttons:{
            'Continue' : {
                keys : ['enter'],
                action : function(){
                    alert('Confirmed'); // if clicked on confirm
                }
            },
            'Cancel': {
                keys : ['esc'],
                action: function(){
                    this.close();
                    alert('Canceled'); // if clicked on cancel
                }
            }
        }
    });
}

JSFiddle

Upvotes: 1

Related Questions