Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQUERY-ui dialog x button function

I'm using the jquery-ui dialog box. My problem is upon clicking the x button to close the dialog, i also need to perform the cancel() function.

How can I do this?

var content = 
{
    autoOpen    : false,
    modal       : true,
    width       : 350,
    minHeight   : 50,
    height      : 350,
    position    : "center",
    resizable   : false,
    draggable   : false,
    close       : function () {$(".privacy_modal").prop("checked", false);},
    buttons: 
    {
        "Cancel": function cancel() 
        { 
            $(".privacy_modal").prop("checked", false); $(this).dialog("close"); 
        },
        "Accept": function accept() 
        {
            $(".privacy_modal").prop("checked", true); $(this).dialog("close"); 
        }
    }
};

TEST

NOTE: Using close doesn't solve my problem because it overrides the function when i clicked the accept button

like image 773
newbie Avatar asked Dec 26 '22 22:12

newbie


2 Answers

You could use a third-party variable (bAccepts which is False by default) and third-party method.

When user accepts:

  • Set bAccepts to True

When user cancels:

  • Set bAccepts to False

When onClose is fired, call the method doClose() which does the following:

  • if bAccepts is True => accept
  • else => cancel

Here is some un-tested psuedo-code. See working code.

var bAccepts = false;
var content = {
                    autoOpen    : false,
                    modal       : true,
                    width       : 350,
                    minHeight   : 50,
                    height      : 350,
                    position    : "center",
                    resizable   : false,
                    draggable   : false,
                    close       : function () { if (bAccepts) {...} else {...} },
                    buttons: {
                        "Cancel": function cancel() { bAccepts = false; $(this).dialog("close");},
                        "Accept": function accept() { bAccepts = true; $(this).dialog("close");}
             }
};
like image 76
jonjbar Avatar answered Dec 29 '22 11:12

jonjbar


Working demo http://jsfiddle.net/Ea6Hm/1/

You can use : http://docs.jquery.com/UI/Dialog#event-beforeClose

using beforeClose you can call any function you want to invoke before the Dialog box close.

Hope this helps,

code

$(document).ready(function() {
    $('#theLink').click(function() {
        $("#forgot-dialog").dialog("open");
    });

    $("#forgot-dialog").dialog({
        modal: true,
        autoOpen: false,
        height: 255,
        width: 300,
        beforeClose: function() {
            alert("Do whatever before Close");
        },
        buttons: {
            "Retrieve": function() {
                document.forms["forgotform"].submit();
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        },
    });


});​
like image 29
Tats_innit Avatar answered Dec 29 '22 12:12

Tats_innit