1

I have an AJAX call as such :

    $('a.delete_task').live('click', function() {
      $this = $(this);

      function deleteFunction(){            

        var obj = $this.parents('.task');
        $(obj).addClass('highlighted');

        $.post($this.attr('href'), { _method: 'delete' }, function(data) {
          if ( $single_item_collection == true ) {
          } else {
          };
        });
      };

      SSK.confirm_delete($this, deleteFunction, "task");
      return false;
    });

And then I take my deleteFunction() and throw it into the delete_confirmation :

$(function(){
  window.SSK = new(Class.extend({
  confirm_delete: function(obj, action, label){
    $(".confirm-deletion").live("click", function(){
      action.call(obj);
      $(this).parents("#delete-message").fadeOut();
      return false;
    });
  }, 

The problem is that when I click it the first time it works. When I click it the second time, it passes through the first $(this), and the second $(this). Likewise, when I click another item for a third time, it tries and pass all three and so on.

Somehow it is caching $(this). As crazy as that is. And passing it everytime the method is passed again.

Confirm delete as a function creates a popup and passes the method of the link you originally clicked to it as the variable obj.

Then if you click confirm it does this :

    $(".confirm-deletion").live("click", function(){
      action.call(obj);
      $(this).parents("#delete-message").fadeOut();
      return false;
    });
1
  • 1
    Can you include a bit more of your code? It sounds to me like you're binding a new click handler every time and so each time an additional handler is attached. Then clicking on it fires all of the clicks. Commented Apr 5, 2012 at 13:28

2 Answers 2

1

You are adding a click event to the element 'confirm-deletion' every single time the user clicks 'delete_task'. That's why the click event is firing multiple times, it's literally been added multiple times.

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

Comments

1

No, it's surely not crazy caching. Read https://developer.mozilla.org/en/JavaScript/Reference/Operators/this and rewrite confirm_delete and/or deleteFunction.

I'm quite sure that you need to put $(this) in a caching value in the click-handler closure, and then use that in deleteFunction.

1 Comment

I turned $(this) into $this, and now it sends the same thing as many times as I have clicked on the page. So instead of the past 3 for the 3rd click, it passes 3 of the current events.

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.