Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  194] [ 7]  / answers: 1 / hits: 84453  / 14 Years ago, tue, august 24, 2010, 12:00:00

I am using jQuery UI dialog to display a confirmation dialog when a button is clicked. I want to return true, when OK is clicked and false otherwise.



Associating dialog open call in onClick (as given here, $dialog.dialog('open');) event does not serve the purpose. So, as a workaround, I followed an approach, which is similar to this: http://www.perfectline.co.uk/blog/unobtrusive-custom-confirmation-dialogs-with-jquery. There are two differences between this approach and mine:




  1. The example uses anchor tag and,

  2. It does not use jQuery UI dialog.



I have modified the code given in the example link, but it does not work. I wonder what I am doing wrong. Is there any cheaper way to do this?



Here is the code, all the CSS/JS are referencing to jQuery CDN, so you should be able to copy the code to see the behavior.



<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>

<head>
<link type=text/css href=http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/blitzer/jquery-ui.css rel=stylesheet />
<script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.min.js></script>
<script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js></script>
<meta http-equiv=Content-Type content=text/html; charset=utf-8 />
<title>Control Panel</title>
</head>

<body>
<form action=http://google.com>
<button class=es-button ui-state-default ui-corner-all name=changeSem id=id2>Start Thermonuclear War</span>
</button>
</form>
<div title=Why so serious? id=id3 style=display:none;>
<p>You are about to start a war.
<p>Click OK to confirm. Click Cancel to cancel this action.</p>
</div>
</body>
<script type=text/javascript>
$('#id2').click(function (event) {

if ($(this).data('propagationStopped')) {
//alert('true');
$(this).data('propagationStopped', false);
return true;
} else {

event.stopImmediatePropagation();

$('#id3').dialog({
//autoOpen: false,
width: 600,
buttons: {
Ok: function () {
$(this).dialog(close);
$('#id2').data('propagationStopped', true);
$('#id2').triggerHandler(event);
},
Cancel: function () {
$(this).dialog(close);
return false;
}
}
});
//alert('false');
return false;
}
});
</script>

</html>





Clarification: Please note that it's very simple (see the solution by bryan.taylor) to do a form submission. What I want to do here is to simulate submission by button click. More like JavaScript's confirm() method.


More From » jquery

 Answers
32

JavaScript is asynchronous. The call to



$myDialog.dialog('open'); 


will not block, but rather will return immediately.



Instead, one must use event callbacks that you provide in the buttons option of dialog.



This near-duplicate provides a good example:



jquery ui dialog box need to return value, when user presses button, but not working


[#95824] Sunday, August 22, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aricl

Total Points: 215
Total Questions: 91
Total Answers: 94

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;