2

I need to open a popup window on clicking a button and used jquery dialog for this.

    $(document).ready(function(){
    $("#dialog-form").dialog({
        autoOpen : false,
        height : 300,
        width : 350,
        modal : true,
        buttons : {
            "Add" : function() {
                $("#tag1").text($("#textArea").val());
                $(this).dialog("close");
            },
            Cancel : function() {
                $(this).dialog("close");
            }
        },
        close : function() {
            $("#textArea").val("");
        }
    });

});

    function openWindow(){
        $("#dialog-form").dialog("open");
        statement1;
        statement2;
        }


<button id="add" onclick="openWindow()">Add</button>

problem over here is when i click the button dialog box is opened, but before even i enter some text in the dialog box statement1 and statement2 are getting executed and then focus is coming to the dialog box.

How can i make the statement1 and statement2 to execute only after dialog box returns?

I don't want to add the statement1 and statement2 to the "Add" function. The reason for not adding the statements in the "Add" function is because i have multiple buttons and each of these should first open the dialog box and then will execute different set of statements.

vjk
  • 2,163
  • 6
  • 28
  • 42

5 Answers5

5

Easy fix would be to use the close callback:

$(document).ready(function () {
    $("#dialog-form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Add": function () {
                $("#tag1").text($("#textArea").val());
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            $("#textArea").val("");
            //statement1 -- won't fire until dialog is closed
            //statement2 -- won't fire until dialog is closed
        }
    });
});

function openWindow() {
    $("#dialog-form").dialog("open");
}

Another thing to consider would be $.Deferred

Aaron Blenkush
  • 3,034
  • 2
  • 28
  • 54
0

I have an example for you:

$(".selector").click(function () {
        var dialog = $('<div title="Title"></div>').dialog({
            open: function (event, ui) {
                $.ajax({
                    url: 'www.google.com.br',

                    cache: false,
                    success: function (html) {
                        $(dialog).html(html);
                    },
                    error: function () {
                        $(dialog).remove();
                        alert("Some Error MSG");
                    }
                });
            },
            close: function () {
                $(dialog).remove();
            },
            resizable: false,
            width: 500,
            modal: true
        });
    });

In this case, the dialog is receiving the HTML result, only after it opens.

Tiago B
  • 1,937
  • 14
  • 34
0

This can be achieved by the following way:-

$('#mydialog').dialog("open");
$('#mydialog').load('serverURL',server_data_variable, function() {
    myfunction();
});

This will execute the function once the dialog loading is done.It will register the callback to be executed post dialog done.The variable server_data_variable is optional and is supposed to be used only if user wants to send some data otherwise it can be skipped as.well.

Ray
  • 3,864
  • 7
  • 24
  • 36
Nitin Vashisth
  • 111
  • 1
  • 2
0

Solution 1: (same as solution from Aaron Blenkush)

$("#dialog-form").dialog({
    autoOpen: false,
    height: 300,
    width: 350,
    modal: true,
    buttons: {
        "Add": function () {
            $("#tag1").text($("#textArea").val());
            $(this).dialog("close");
            //statement1 -- will fire only if "add" button is clicked
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    },
    close: function () {
        $("#textArea").val("");
        //statement1 -- will fire after dialog is closed
    }
});

Solution 2 is to make a promise:

const dialogPromise = new Promise(function (resolve, reject) {
    $("#dialog-form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Add": function () {
                $("#tag1").text($("#textArea").val());
                $(this).dialog("close");
                resolve(true);
            },
            Cancel: function () {
                $(this).dialog("close");
                resolve(false);
            }
        },
        close: function () {
            $("#textArea").val("");
        }
    });
});
const addClicked = await dialogPromise; 
Szynkie
  • 59
  • 3
-1

Call callback function after "open" clause in the dialog setup.

 modal: true,
 resizable: false,
 resize: 'auto',
 close: dialogCloseFunction,
 **open: function(){if(itemid) {showDailogSides(itemid);}** if(!siteParams[3]) {$(".detailSideClass").hide(); $(".addToChartinDialog").hide();}},
 //hide: {effect: "fadeOut", duration: 5000}
 show: { effect: "fade", duration: 1000 } //drop blind fade fold slide clip
abc
  • 1