Referring to the triggering object in the done() block of a post() call?

You can use the var that = this workaround or just use ES5 .bind function to set the this correctly!

jQuery('.vote .magic_button').click(function() {
  jQuery.post(url, {
      action: 'ajax_vote',
      'vote_target': jQuery(this).data('postid')
    })
    .done(function(data) {
      // $img = jQuery(this).find('img');         
    }.bind(this))
});

Documentation

Leave a Comment