tags:

views:

447

answers:

3

I have a link like

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

And then I have some jQuery:

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

But when I click cancel in the alert box it still goes to www.site.com in stead of doing nothing. How to solve this?

+1  A: 

Return the bool from confirm() ?

(Not much of a jQuery wizz, though :) )

jensgram
You have the right idea +1
Jose Basilio
+4  A: 

Add the return statement:

$(".cancel").click(function(){
    return confirm("sure??");
});
Ilya Birman
beat me by a second. +1
Jose Basilio
Usually this happens to me :-)
Ilya Birman
+1  A: 

returning bool is the old way to do it but is not the preferred method in every browser and I have had many problems with it these days. jQuery wraps the event object and handles the event canceling for you.

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

$(".cancel").click(function(event){
    if (!confirm("Sure??"))
       event.preventDefault();
});
Chad Grant