views:

321

answers:

3

For some weird reason i'm getting my confirm box coming up twice. here is my code:

$(".DeleteComment").live("click", function(){

    var CommentID = $(this).attr("rel");
    var confirm

    if (!confirm('Are you sure you want to permanently delete this comment?')){

     return false;

    }else{
     $(this).html("loading").css("color", "#999");
     //AJAX HERE
     return false;
    }


});
A: 

Did you try removing that not-used var confirm?

Marcel J.
oops, tried removing it, no luck.
Adam
+2  A: 

Do you load any content dynamically (via ajax)? It could be the case that the click event is bound to the same element twice resulting in the double confirmation.

Marek Karbarz
A: 

try this:

$_blockDelete = false;

$(".DeleteComment").live("click", function(e){

    e.stopPropagation();

    if (!$_blockDelete)
    {
      $_blockDelete  =true;
      var rconfirm = confirm('Are you sure you want to permanently delete this comment?');
      if (rconfirm)
      {
          $(this).html("loading").css("color", "#999");
          var CommentID = $(this).attr("rel");
          //AJAX HERE
          //return the value "false" the variable "$_blockDelete" once again ajax response

      }
    }
    return false;
});
andres descalzo
from jQuery doc on stopPropagation() "Note that this will not prevent other handlers on the same element from running" so if "click" is bound twice the confirm dialog will still show up twice.
Marek Karbarz
ok, I agree with you that in this case would not be useful. better to leave the example modify the script.
andres descalzo