views:

112

answers:

2

How can I achieve this?

  1. A user clicks the delete link (with a class of "confirm").
  2. Confirmation message appears asking "Are you sure?" with "Yes" and "Cancel" options.

If yes is selected, the link continues as clicked, but if cancel is selected, the action is canceled.

Update: Final working code with confirm() thanks to this guy:

$('.confirm').click(function() {
    return confirm("Are you sure you want to delete this?");
});
+8  A: 

Javascript provides a builtin confirmation dialog.

if (confirm("Are you sure?"))
{
    // continue with delete
}
Joel Potter
Will give you OK and Cancel options.
Robert Harvey
can't beat it for easiness.
nickf
The simplest answer is usually the correct one :)
AntonioCS
@Robert: true, but the OP wants the "Easiest way". It's close.
Joel Potter
This is the easiest option, but remember that it is impossible to style the dialog at all. I'm not too familiar with jquery, but it looks like Robert already linked to a jquery plugin with styling options.
Johrn
Thanks, but can you also provide the code to disable the click so it doesn't continue after clicking cancel?
Andrew
@Andrew: `confirm()` returns a boolean which matches the button selected. Just put whatever you want confirmed inside the if statement and will not execute if the user clicks cancel.
Joel Potter
is 'return false;' the appropriate way to disable the click event?
Andrew
solved it! I needed return confirm(); instead of if(confirm()){}
Andrew
+1  A: 

http://nadiana.com/jquery-confirm-plugin

Robert Harvey
I like that plugin. Nice and simple. Thanks for sharing!
Andrew