8

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?");
});
2
  • did you put it in a $(document).ready(){}? Commented Dec 4, 2009 at 0:52
  • Yes, it's in my document ready. Commented Dec 4, 2009 at 0:56

2 Answers 2

17

Javascript provides a builtin confirmation dialog.

if (confirm("Are you sure?"))
{
    // continue with delete
}
Sign up to request clarification or add additional context in comments.

7 Comments

Will give you OK and Cancel options.
@Robert: true, but the OP wants the "Easiest way". It's close.
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.
Thanks, but can you also provide the code to disable the click so it doesn't continue after clicking cancel?
@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.
|
3

in my experience this is the best and easiest way to have a confirmation!

<a href="#" onclick="return myconfirm()">Confirm</a>
<script>
function myconfirm()
{
    if(confirm('Are You Sure ...'))
        return true;
    return false;
}
</script>

1 Comment

an even easier way would be to use the return value of the confirm function: onclick="return confirm('Are you sure?')"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.