Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to create a confirmation message with jQuery/JavaScript?

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?");
});
like image 351
Andrew Avatar asked Dec 03 '09 23:12

Andrew


2 Answers

Javascript provides a builtin confirmation dialog.

if (confirm("Are you sure?"))
{
    // continue with delete
}
like image 136
Joel Avatar answered Oct 11 '22 01:10

Joel


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>
like image 3
Nima Ghaedsharafi Avatar answered Oct 10 '22 23:10

Nima Ghaedsharafi