go there And then I have jQuery: $(".cancel")...">

Cancel functionality in href

I have a link like

<a href="www.site.com"  class="cancel">go there </a>

And then I have jQuery:

$(".cancel").click(function(){
        confirm("sure??");
 });

But when I click cancel in the warning window, it still goes to www.site.com instead of doing nothing. How to solve this?

+3
source share
4 answers

Add a return statement:

$(".cancel").click(function(){
    return confirm("sure??");
});
+6
source

return bool is an old way to do this, but is not the preferred method in every browser, and these days I had a lot of problems. jQuery wraps an event object and handles event cancellation for you.

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

$(".cancel").click(function(event){
    if (!confirm("Sure??"))
       event.preventDefault();
});
+8
source

bool ()?

( jQuery wizz, :))

+1

.

event.preventDefault (); is the correct fix. I tried to return false for the click event on the anchor tag, and this will not work, because I called window.location first. By setting event.Default () as the first line in my handler, I could create a window and open it, preventing href from executing.

+1
source

Source: https://habr.com/ru/post/1707024/


All Articles