Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am using jQueryUi dialog with 2 buttons as a replacement of confirm(msg) in javascript.
initializing the jqueryUI dialog:
JavaScript
$(function() {
    	$('#id').dialog({
        	autoOpen: false,
        	width: 600,
        	buttons: {"Yes": yes, "No": no},
        	modal: true    
        });
     });
function yes(){    	
    	$('#id').dialog( "close" );
    	 return true;
	}
function no(){  	
    	$('#id').dialog( "close" );    	
    	return false;
	}

In jqueryUI there is 2 steps:
1.
JavaScript
$("id").dialog('open');// which opens the dialog

2. based on the button click returns true/false.

I am opening this dialog from a function Open() in a another JS file.
How can I have exact functionality as confirm(msg), ie the control waits till the user clicks on a button and returns true/false?
Open()
{......
//var choice = confirm("MSG");<-- returns true/false based on the button click
..opens the jqueryUI dialog
..blocks the control till the user clicks on the buttons
var choice = <-- returns true/false based on the button click
....
}


Any help is appreciated.
Thanks and Regards,
Sayan.
Posted

1 solution

Problem

JavaScript is Asynchronous.

Solution

In order to get the values, you have to call one CallBack function.

So, modify your Code like below.
JavaScript
$('#id').dialog({
    autoOpen: false,
    width: 600,
    modal: true,
    buttons: {
        "Yes": function () {
            $(this).dialog('close');
            callback(true);
        },
        "No": function () {
            $(this).dialog('close');
            callback(false);
        }
    }    
});

function callback(value) {
    if (value) {
        alert("Confirmed");
    } else {
        alert("Rejected");
    }
}


Demo

[Demo] jQuery UI Confirm Dialog Box[^]
 
Share this answer
 
v3
Comments
Sayan Bera 15-Nov-13 8:20am    
Thanks for the quick reply.
I tried exactly as mentioned on the demo link. But its not solving my purpose.
in Open() function i did as follows...
Open()
{
.....
var r = fnOpenNormalDialog();//I require to wait here till the user selects Yes/No and get the returned value
if(r == false)
return;
.... else it proceeds below
....
}
I want the mechanism exactly as confirm(msg) does.
As I said you can't get value like this. You have to handle this in callback function.

So, do like below.

Open()
{
fnOpenNormalDialog();
}

function callback(value) {
if (!value) {
return;
} else {
// Proceed with your logic.
}
}
Did you try?
Sayan Bera 15-Nov-13 10:01am    
Yes. But the thing you mentioned is having the logic in a separate function and not in Open().
Well, I'm having another problem in that way(SharePont related), so I need to post that separately. The thing I tried seems to be a no-go.
Thanks very much for your replies.
Yes, you can't do in the same function as this is Asynchronous.

Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Tadit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900