0

I have a link that I would like to present with a confirmation. I am using the javascript confirm() method. But the only way for me to get the link to not work when the user clicks 'cancel' is to use return false;. Is that the correct way to do it (cross-browser)?

$('a.confirm').click(function() {
    if(confirm('Are you sure? This cannot be undone.')) {
        return true;
    }
    return false;
});

2 Answers 2

3

Returning false on an event handler, is equivalent to call both event.preventDefault and event.stopPropagation, your code should work, but what about:

$('a.confirm').click(function() {
  return confirm('Are you sure? This cannot be undone.');
});

It will return false if the user cancels the confirm...

Run that snippet here.

Sign up to request clarification or add additional context in comments.

1 Comment

I knew there was a better way to do it! Thanks!
0

See preventDefault: http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

$("a").click(function(event){
  event.preventDefault();
});

Comments

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.