How to disable click using jQuery / javascript?

I have a link that I would like to submit with confirmation. I am using the javascript confirm() method. But the only way to make the link not work when the user clicks cancel is to use return false; . Is this the right way to do this (cross browser)?

 $('a.confirm').click(function() { if(confirm('Are you sure? This cannot be undone.')) { return true; } return false; }); 
0
javascript jquery
source share
2 answers

Returning false in the event handler is equivalent to calling 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 confirmation ...

Run this snippet here .

+3
source share

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

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

All Articles