jQuery UI dialog window - how to not execute script if user clicks cancel

jQuery UI dialog window - how to not execute script if user clicks cancel
0

#1

Here we have one simple modal window, how can I take action based on what is inside the variable ajaxReq.

window.ajaxReq = true;
$( “#dialog-confirm” ).dialog({
resizable: false,
height: “auto”,
width: 400,
modal: true,
buttons: {
“Confirm”: function() {
$( this ).dialog( “close” );
},
“Cancel”: function() {
window.ajaxReq = false;
$( this ).dialog( “close” );
}
}
});

if(ajaxReq == false) { return; }

The ajaxReq here above is evaluated as true, I wonder what is the technique for initializing the variable, but getting the data after somebody clicks cancel.


#2

If tou use Redux you can take action on the changes of state which can include this variable that you care about.


#3

Note: when you include code blocks in a post, enclose them between three backticks (```) at the beginning and the end to keep the formatting, e.g.,

Some code
      More code
           Even more code

You can define functions to be executed when a dialog is opened or closed, e.g.,

window.ajaxReq = true;
$( “#dialog-confirm” ).dialog({
	resizable: false,
	height: “auto”,
	width: 400,
	modal: true,
	open: function () {
		openFunction();
	},
	close : function() {
		closeFunction();
	},
	buttons: {
		"Confirm": function() {
			$( this ).dialog( “close” );
		},
		"Cancel": function() {
			window.ajaxReq = false;
			$( this ).dialog( “close” );
		}
	}
});

function openFunction() {
	alert("The dialog is open.");
}

function closeFunction() {
	alert("The dialog is closed.");
	if(ajaxReq == false) { return; }
}

and do whatever you need to within the closeFunction, in this case.