1

I am showing a Delete confirmation dialogue box, and I want the result of User's click of YES or NO as TRUE or FALSE

Now, since Javascript function is asynchronous, the variable yesDelete in the code below does not wait for the users choice of confirmation.

Now, is there a way to return the value r as the returnvalue of the function ShowDeleteConfirm()?

function ShowDeleteConfirm() { 
    var yesDelete=false;

    jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {

        yesDelete = r;

    }); 

    return yesDelete;

}

2 Answers 2

1

One way to do it would be to pass a callback function as an argument either to ShowDeleteConfirm() or to jConfirm(). Something like this:

function ShowDeleteConfirm(callback) {

    //someone correct me if I'm out of scope here...
    jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {

        callback(r);

    });
}

I think that will work, but I'm not sure if callback is in the scope of jConfirm's callback. Worth a shot though :)

The otherway to do it would be to have the jConfirm callback function set a property on the window object where it's globally visible.

Anyway, hope that helps a little at least...

EDIT

Because JS is asynchronous, I'm don't think you can have ShowDeleteConfirm() return true or false based on the users input. Hence the need to pass in a different callback function: ShowDeleteConfirm(yourCallbackFunctionHere). You could do this:

ShowDeleteConfirm(function(result) {
    //result should be true or false
});

but I think that's as close as you're going to get. If that doesn't work for you, then you should probably think about changing your design to accommodate the fact that JS is asynchronous.

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

+1 - This should work. Here is a fiddle that shows this technique using a jQuery UI dialog. jsfiddle.net/jeWkB/3
Thank you Ian for your reply. It is almost there. But when I fiddled, both OK and Cancel buttons returned TRUE. When I call, say "var result=ShowDeleteConfirm()", the variable "result" should be either true or false. That is all.
Thank you very much for your replies. Anyway, what you gave me was of use. But still, I will try another login.
0

I think following will be ur case:

 if (ShowDeleteConfirm()){
 myFunc();
    }

function ShowDeleteConfirm() { 

var yesDelete=false;

jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {

    yesDelete = r;
 }); 

return yesDelete;

}

should be like this:

ShowDeleteConfirm(myFunc);

        function ShowDeleteConfirm(myFunc) {     

        jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {    
if (r){            
myFunc();
}
         }); 


        }

1 Comment

Hi Brij,I think you are very close to my solution. Suppose I say var yes = ShowDeleteConfirm(); the value of YES should be TRUE or FALSE depending on users click. When I tried, the return value was undefined. When I give an alert, the returnvalue is given even before the dialogue box is displayed. (An asynchronous operation) Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.