My problem is that my variable flag
is not getting updated some how, I have even tried to do that with setTimeout
function but no luck!
JavaScript
function confirmed(msg, okFtn, cnclFtn){
flag=false;
$(document.createElement('div')).html('\
<div style="max-width:250px;width:100%;">'+msg+'\
<p>\
<button value="yes" class="button small gradient blue left rnd5 cnfrm">OK</button>\
<button value="no" class="button small gradient blue left rnd5 cnfrm">Cancel</button>\
</p></div>')
.modal({
onShow: function (dialog) {
$('.simplemodal-close').remove();
$("#simplemodal-container").css({'height': 'auto','width':'auto'});
$(window).trigger('resize.simplemodal');
},
onClose: function(){
console.log(flag);
if(flag)
return true;
else
return false;
}
});
$('button.cnfrm').click(function(){
flag=true;
if ($(this).val() == 'yes') {
if(okFtn && typeof okFtn === 'function')
okFtn();
} else {
if(cnclFtn && typeof cnclFtn === 'function')
cnclFtn();
}
setTimeout(function(){$.modal.close();}, 1000);
});
}
This is Fiddle link of what I am saying.
Since variable is not setting up so $.modal.close()
is not working fine, what I want is just closing modal dialog if and only if button OK
or cancel
is clicked.
http://www.ericmmartin.com/projects/simplemodal/
From the documentation:
onClose: Useful for adding effects to the closing of the modal dialog elements. After you’ve applied effects, etc., you’ll need to call $.modal.close(); so SimpleModal can re-insert the data correctly and clean up the dialog elements.
You need to include a call to $.modal.close()
in your onClose()
handler, e.g.
onClose: function(){
console.log(flag);
if(flag) $.modal.close();
}
ps. As for the flag
variable, it's getting set up exactly as you expected it to. It's not the problem.