Prevent a link, confirm and then goto location jquery

Viewed 39399

I have links like this.

<a href="delete.php?id=1" class="delete">Delete</a>

If a user click on it. A confirmation should popup and then only if user click yes, it should goto the actual url.

I know that this can prevent the default behavior

    function show_confirm()
    {
    var r=confirm("Are you sure you want to delete?");
    if (r==true)   {  **//what to do here?!!** }

    }    


    $('.delete').click(function(event) {
    event.preventDefault();
    show_confirm()
    });

But how do i continue to that link or send an ajax post to that link after confirming?

10 Answers

This is a short form:

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

I use it on web sites for download/link confirmation.

In my case, jQuery wont run, so I take this solution for me:

<button onclick="show_confirm('delete?id=1')">&times;</button>

And

function show_confirm(obj) {
  var r = confirm("Are you sure you want to delete?");
  if (r === true)
    window.location.replace(obj);
}
Related