22

I'v got a jquery ui dialog box I want to use to prompt the user to confirm a deletion. When the user presses "yes" or "no" I need to return "True" or "False" to continue some javascript execution. The problem with the code below is when the dialog box shows up it immediately is executing a "return true;" but the user hasn't pressed the "Yes" button yet?

What am I doing wrong?

HTML:

<div id="modal_confirm_yes_no" title="Confirm"></div>

Javascript call:

$("#modal_confirm_yes_no").html("Are you sure you want to delete this?");
var answer = $("#modal_confirm_yes_no").dialog("open");
if (answer)
{
 //delete
}
else
{
 //don't delete
}

Jquery dialog:

$("#modal_confirm_yes_no").dialog({
 bgiframe: true,
 autoOpen: false,
 minHeight: 200,
 width: 350,
 modal: true,
 closeOnEscape: false,
 draggable: false,
 resizable: false,
 buttons: {
 'Yes': function(){
 $(this).dialog('close');
 return true;
 },
 'No': function(){
 $(this).dialog('close');
 return false;
 }
 }
 });
Naftali
147k41 gold badges247 silver badges304 bronze badges
asked May 18, 2011 at 18:57
1
  • It looks like you are missing a quote in your first block of code. I don't know if that will fix your issue but if you copy and pasted your current code, that is going to cause problems. Commented May 18, 2011 at 19:01

5 Answers 5

38

javascript is asynchronous.

so you have to use callbacks:

 $("#modal_confirm_yes_no").dialog({
 bgiframe: true,
 autoOpen: false,
 minHeight: 200,
 width: 350,
 modal: true,
 closeOnEscape: false,
 draggable: false,
 resizable: false,
 buttons: {
 'Yes': function(){
 $(this).dialog('close');
 callback(true);
 },
 'No': function(){
 $(this).dialog('close');
 callback(false);
 }
 }
 });
 function callback(value){
 //do something
 }
answered May 18, 2011 at 19:00

3 Comments

I'm having some issues with replicating this -- for instance, I want to utilize this function later, depending on which button the user clicks. Do I just add 'callback(true)' and 'callback(false' to the buttons and place the function inside of the function I want to call? I'm a newbie when it comes to callbacks!
I need it to return true or false because so I can prevent a tab switch in jquery UI tabs using the select: option... this callback option isn't going to work for me unfortunately. Any ideas?
Another Solution if you want return in a way: stackoverflow.com/a/18474005/1876355
2

U should see this answer.

Well, This can work.

Your dialog function... showDialog()

function confirmation(question) {
 var defer = $.Deferred();
 $('<div></div>')
 .html(question)
 .dialog({
 autoOpen: true,
 modal: true,
 title: 'Confirmation',
 buttons: {
 "Yes": function () {
 $(this).dialog("close");
 defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false.
 },
 "No": function () {
 $(this).dialog("close");
 defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false.
 }
 },
 close: function () {
 $(this).remove();
 }
 });
 return defer.promise();
}

and then the code where you use the function...

function onclick(){
 var question = "Do you want to start a war?";
 confirmation(question).then(function (answer) {
 if(answer=="true"){
 alert("this is obviously " + ansbool);//TRUE
 } else {
 alert("and then there is " + ansbool);//FALSE
 }
 });
}

This may seem wrong for most people. But there is always some situations where you just can't go without return from JQuery Dialog.

This will basically mimic the confirm() function. But with ugly code and a nice confirm box look :)

I hope this helps some people out.

Honestly,I was take many time for this, finally i found best solution.you can see more detail here=>Awesome Solution

answered Nov 11, 2016 at 10:09

Comments

1

If anyone needs a graphic demonstration of asynchronous behavior, here's one that might be helpful. Wrap Ronedog's dialog in a function. I've used "showConfirm" below. Capture the return value in a variable. Call it in some button click event, and try to alert what button was pressed:

$('.btn').on('click', function(event) {
 var cVal = showConfirm('Really?');
 alert('User pressed ' + cVal); 
});

You will find that you always get the alert before you have a chance to press the button. Since javascript is asynchronous, the alert function doesn't have to wait for the result of the showConfirm function.

If you then set up the function Neal's way everything will work (thanks Neal).

answered Aug 30, 2013 at 16:03

Comments

0

You need to use callback functions. Here is the working example:

Following is the fa-icon. When user clicks it it calls javascript.

 <a href="#" id="removeNode"><i class="fa fa-minus-circle fa-lg"></i></a>

Following is the javascript code executed when user click "Remove Node" fa icon.

$("a#removeNode").click(function(){
 // returns the attribute of "selected" Table Row either it is expanded or collapsed based on it's class 
 var datattid = $("tr.expanded.selected, tr.collapsed.selected").attr("data-tt-id"); 
 //alert($("tr.expanded.selected").attr("data-tt-id")); 
 if(typeof datattid != 'undefined'){
 //alert(datattid);
 displayConfirmDialog("You are trying to remove selected node : " + datattid + ". Are you sure?", proceedToRemoveNode);
 }else 
 {
 showErrorDialog("Invalid Row Selection", "Node is not selected.\n You must select the node to remove it." );
 // displayMessage ("#dialog-message", "Invalid Row Selection", "ui-icon-heart", "Node is not selected.\n You must select the node to remove it." ); 
 }
});

The displayConfirmDialog displays the confirmation message and based on use action it calls the callback function. Here is the code of displayConfirmDialog.

//Confirmation dialog to remove node
function displayConfirmDialog(message, proceedWithRequest)
{
 var retVal;
 $("div#dialog-confirm").find("p").html(message);
 var confirmDlg = $( "#dialog-confirm" ).dialog({
 resizable: false,
 height: "auto",
 width: 400,
 modal: true,
 buttons: {
 "Remove Node": function() {
 this.retVal = true;
 proceedWithRequest() 
 $( this ).dialog( "close" );
 },
 Cancel: function() {
 this.retVal = false;
 $( this ).dialog( "close" );
 }
 },
 show:{effect:'scale', duration: 700},
 hide:{effect:'explode', duration: 700} 
 });
 confirmDlg.dialog("open"); 
}

Following is the callback function:

//Callback function called if user confirms to remove node.
function proceedToRemoveNode()
{
 var datattid = $("tr.expanded.selected, tr.collapsed.selected").attr("data-tt-id"); 
 $("#nh_table").treetable("removeNode", datattid);
 showErrorDialog("Node removed successfully", "The node " + datattid + " is removed successfully" );
// alert("The node " + datattid + " is removed successfully");
}

Following is images of working application that deletes node from a TreeTable using JQuery.

enter image description here

After confirmation the node "qa-2" is removed from the Tree as shown in following image.

enter image description here

answered Jun 10, 2017 at 14:57

Comments

-1
function confirm() {
 $("#dialog-message").dialog({
 modal : true,
 buttons: {
 "Yes" : function() {
 $(this).dialog("close");
 document.forms[0].action = "actionname-yes";
 document.forms[0].submit(); 
 },
 "No" : function() {
 $(this).dialog("close");
 document.forms[0].action = "actionname-no";
 document.forms[0].submit();
 } 
 }
 });
JNYRanger
7,11712 gold badges55 silver badges85 bronze badges
answered Mar 13, 2015 at 22:54

1 Comment

Welcome to SO. You are answering with a code only answer on a question that's over 4 years old with an answer accepted with over 30 votes. Additionally this doesn't answer why OP has the issue he/she is experiencing.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.