How to detect confirm() choice?

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?

Upvotes: 0

Views: 1832

Answers (5)

Krishanu Dey
Krishanu Dey

Reputation: 6406

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

Upvotes: 0

Risse
Risse

Reputation: 491

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
   }
});

Upvotes: 1

Jason Jong
Jason Jong

Reputation: 4330

Try this

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

    if (reply)
    {
        // your OK
    }
    else
    {
        // Your Cancel
    }
});

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318568

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

if(confirm('Are you sure?! DAMN sure?')) {
    // yes
}
else {
    // no
}

Upvotes: 4

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

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
}

Upvotes: 5

Related Questions