views:

66

answers:

2
+3  Q: 

jquery click event

Hi,

I have a delete button on my web site which I want to add a confirm box to. I have wrote the following code in JQuery but I'm unsure how to continue with the page load if the user confirms the delete. e.g. what do I put inside the if statement to reverse the preventDefault?

$(".delete").click(function(e){
            e.preventDefault(); 

            if (confirm('Are you sure you want to delete this?')) {
                 NEED SOMETHING IN HERE TO CONTINUE WITH THE LOADING OF THE PAGE

            }
        });

Thanks

+3  A: 

e.preventDefault() has the same function as simple return false, so you can do this to achieve the same effect:

$(".delete").click(function(e){
    // Will continue if the user clicks 'Yes'
    return confirm('Are you sure you want to delete this?');
});

Don't know what you're trying to do but this should answer your question.

Note that this function cannot stop the page from loading, as when this function is called, the page has already loaded. So give an example of what you're trying to do if you need a more specific answer.

Tatu Ulmanen
cheers worked perfectly.
John
+1  A: 

Just change the logic?

$(".delete").click(function(e){
    if (!confirm('Are you sure you want to delete this?')) {
        e.preventDefault();           
    }
});
Ross