John
John

Reputation: 21927

jquery click event

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

Upvotes: 3

Views: 380

Answers (2)

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

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.

Upvotes: 3

Ross
Ross

Reputation: 14415

Just change the logic?

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

Upvotes: 1

Related Questions