4

You may find solution over this in many posts(Post 1 , Post2 ), but their solution not working for me.

Here is the normal jquery dialog box written by me.

$("#dialog").dialog({
        autoOpen:false,
        buttons:{
            "ok":function(){                                        
                        $(this).dialog("close"); 
                        return true;                                                                    
                    },
            "cancel":function(){                          
                        $(this).dialog("close");     return false;                  
                }
            }   
});

I will open the dialogbox with code:

var returnVal=$("#dialog").dialog("open");

I need to return false,if user clicks 'cancel' and return true if user clicks 'ok'.

var returnVal=$("#dialog").dialog("open");

I NEED returnVal to return boolean value(true/false), but it returns javascript object.

Community
  • 1
  • 1
Umesh Patil
  • 10,475
  • 16
  • 52
  • 80

2 Answers2

8

You cannot return something from the OK / cancel functions as they are essentially event handlers that are only processed upon the click of a button.

Use a separate function to process the result :

$mydialog = $("#dialog").dialog({
    autoOpen: false,
    buttons: {
        "ok": function() {
            $(this).dialog("close");
            processResult(true);
        },
        "cancel": function() {
            $(this).dialog("close");
            processResult(false);
        }
    }
});

$mydialog.dialog("open");


function processResult(result) {
    alert(result);
}

Working example : http://jsfiddle.net/nz2dH/

Jesse Adam
  • 415
  • 3
  • 14
Manse
  • 37,765
  • 10
  • 83
  • 108
  • 1
    sorry ManseUK (I wanted to accept it with Big Thanks !) but wait wait.. I working over it on my page.. I just provided little code to you here. I am not able to propogate 'result' where form is submitted. – Umesh Patil Jan 05 '12 at 09:57
  • @Umesh I wanted you to read that page and accept other answers too - not for this question ... can you create a http://jsfiddle.net/ with your problem – Manse Jan 05 '12 at 10:00
  • off course ManseUK.. it will take some time. can you please wait for me..? – Umesh Patil Jan 05 '12 at 10:10
5

I have implement Yes/No confirmation dialog with custom message and callback function like this. This is useful, if you like to use the same dialog for various purposes.

<script type="text/javascript">
    // prepare dialog
    $(function () {
        $("#confirm-message-dialog").dialog({
            autoOpen: false,
            modal: true,
            closeOnEscape: false,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $(this).data("callback")(true);
                },
                No: function () {
                    $(this).dialog("close");
                    $(this).data("callback")(false);
                }
            }
        });
    });

    // open dialog with message and callback function
    function confirmMessageDialog (message, callback) {
        $('#confirm-message-dialog-message').text(message);
        $('#confirm-message-dialog').data("callback", callback).dialog("open");
    };
</script>

<!-- The dialog content -->
<div id="confirm-message-dialog" title="Warning"> 
    <p id="confirm-message-dialog-message"></p>
</div>

Hope that this helps others as well :)

Ilkka
  • 71
  • 1
  • 2