1

Im trying to open a Jquery confirm box using following code.

var a = $('#confirm')                
            .data("x","defaultValue")
            .dialog('open');

alert(a.data("x"));

Inside the dialog I tried to change the value of x.

    $("#confirm").dialog({
    resizable: false,
    autoOpen: false,
    height: 180,
    width: 400,
    modal: true,
    buttons: {
        "Leave the page": function() {                
            $(this).data("x","this is a test");                
            $(this).dialog("close");
        },
        Cancel: function() {
            $(this).dialog("close");
        }
    }
});

How can I get the modified value for x. at the moment the alert shows "DefaultValue". But want to get the "This is a test".

Any thoughts?

PS: I just can't redirect using window.open() inside the dialog.

Hello World
  • 328
  • 4
  • 12

3 Answers3

1

Finally managed to solve this problem. Just thought to post it here, if anyone needs it.

 $("#confirm").dialog({
    resizable: false,
    autoOpen: false,
    height: 180,
    width: 400,
    modal: true,
    buttons: {
        "Leave the page": function () {
            var anchorId = $(this).data("anchorId");
            var formId = $(this).data("formId");

            if (typeof anchorId !== "undefined")
                window.location.assign(anchorId);
            if (typeof formId !== "undefined")
                $(formId).submit();
            $(this).dialog("close");
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

And I open the confirm dialog

 $("a").live("click", function () {       
    if (hasUnsavedData()) {
        $('#confirm')
         .data("anchorId", this)
         .dialog('open');
        return false;
    } else return true;
});
Hello World
  • 328
  • 4
  • 12
0

You can't, I mean at least outside the dialog, "alert" executes before "Leave the page". You need to alert right after "dialog close" call.

$(this).data("x","this is a test");
$(this).dialog("close");
alert($(this).data("x"));
Ergec
  • 11,608
  • 7
  • 52
  • 62
0

SOLUTION:

Jquery Dialog Confirmation with return

This was hard to find, use it wisely

Community
  • 1
  • 1
Pierre
  • 8,397
  • 4
  • 64
  • 80