I have this jquery code that generates a yes/no dialog box when the user click a button.
$('#btnFinalize').click(function()
{
confirm("Are you sure?");
});
But how can I detect if the user click yes/no?
I have this jquery code that generates a yes/no dialog box when the user click a button.
$('#btnFinalize').click(function()
{
confirm("Are you sure?");
});
But how can I detect if the user click yes/no?
Share Improve this question edited Sep 5, 2012 at 17:02 Gordon 317k76 gold badges546 silver badges565 bronze badges asked May 20, 2012 at 12:08 John Micah Fernandez MiguelJohn Micah Fernandez Miguel 5243 gold badges12 silver badges29 bronze badges 2-
if this is all your code, all you need is to add a
return
:.click(function() { return confirm("Are you sure?") })
– georg Commented May 20, 2012 at 12:26 - see plugin and example here: stackoverflow./questions/6457750/form-confirm-before-submit/… – user1662816 Commented Sep 11, 2012 at 12:20
5 Answers
Reset to default 5confirm()
returns true
if the user clicked "yes" ; and false
if he clicked "no".
So, basically, you could use something like this :
if (confirm("Are you sure?")) {
// clicked yes
}
else {
// clicked no
}
Check the return value of confirm()
; it will be true
or false
:
if(confirm('Are you sure?! DAMN sure?')) {
// yes
}
else {
// no
}
Try this
$('#btnFinalize').click(function()
{
var reply = confirm("Are you sure?");
if (reply)
{
// your OK
}
else
{
// Your Cancel
}
});
confirm()
will return true or false, depending on the answer. So you can do something like:
$('#btnFinalize').click(function()
{
var finalizeAnswer = confirm("Are you sure?");
if(finalizeAnswer) {
// do something if said yes
} else {
// do something if said no
}
});
'Confirm()' returns 'true' if you press 'yes', and 'false' for 'no'.