0

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?

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • if this is all your code, all you need is to add a `return`: `.click(function() { return confirm("Are you sure?") })` – georg May 20 '12 at 12:26
  • see plugin and example here: http://stackoverflow.com/questions/6457750/form-confirm-before-submit/12357337#12357337 –  Sep 11 '12 at 12:20

5 Answers5

5

confirm() 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
}
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
4

Check the return value of confirm(); it will be true or false:

if(confirm('Are you sure?! DAMN sure?')) {
    // yes
}
else {
    // no
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
2

Try this

$('#btnFinalize').click(function()
{
   var reply = confirm("Are you sure?");         

    if (reply)
    {
        // your OK
    }
    else
    {
        // Your Cancel
    }
});
Jason Jong
  • 4,310
  • 2
  • 25
  • 33
1

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
   }
});
Risse
  • 493
  • 1
  • 4
  • 8
0

'Confirm()' returns 'true' if you press 'yes', and 'false' for 'no'.

Krishanu Dey
  • 6,326
  • 7
  • 51
  • 69