MKN Web Solutions MKN Web Solutions - 1 month ago 17
Javascript Question

Javascript - Custom Confirm Dialog - replacing JS confirm

This may be a easy answer-

In my JS I've replaced JS's confirm function with my own. Which basically and simply looks like this:

function confirm(i){
var options = '<br/><br/><input class="button1" value="Yes" type="button" onclick="return true"> <input class="button1" value="No" type="button" onclick="return false">';
$('#text').html(i+options);
$('#confirmDiv').fadeIn('fast');
}


Obviously the return true / false didn't work, or else I wouldn't be asking!

In another function i've got (So you get the picture):

var con = confirm("Are you sure you'd like to remove this course?");
if(!con){return;}


How can I get confirm to return the value directly? I'd assume it's return {this.value} or so?

Thanks!

Answer

Your problem is that your custom confirm isn't modal. That means that when your confirm is shown, the code runs on. There is no chance for you to wait for the user's choice within confirm() and return it from there.

As far as I know, there is no way to emulate the behaviour of a modal confirmation dialog in Javascript (except for the non-standard ShowModalDialog().)

The usual way of doing this is adding a function() { ... } callback to each button's click event, and doing whatever the "ok" click is supposed to do in there.