0

It it my code

$(".library-fields-container div.field-content img").click(function(){
    var nid = getImageNodeID($(this).siblings(".image-span").text());
    $.ajax({
        url: Drupal.settings.basePath + 'views/ajax',
        type: 'POST',
        dataType: 'json',
        data: 'view_name=fehrest&view_display_id=block_1&view_args='+nid,
        success: function(response) {
            var output = response[1].data;
            alert(output);  
            $(this).hide();
        },
        error: function(data) {
            alert('An error occured!');
        }
      });
});  

I'm sure the output variable prints something and it is not empty, because the alert function displays it's content.

The problem is it doesn't hide the $(this) while when I put the

$(this).hide();

At the beginning of the click event just before the Ajax call it hides the image.

What is the Ajax callback function with $(this) object?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
M a m a D
  • 1,938
  • 2
  • 30
  • 61
  • Also a duplicate of [$(this) inside of AJAX success not working](http://stackoverflow.com/q/6394812/218196). Please use the search before asking a new question. – Felix Kling Jun 07 '15 at 06:34

2 Answers2

1

In this context - this is not an element. Save before ajax call this to self variable. Like

var self = this;
$.ajax({
    ...
    success: function(response) {
        var output = response[1].data;
        alert(output);  
        $(self).hide();
    }
});
Alexey Nis
  • 471
  • 3
  • 9
0
$(".library-fields-container div.field-content img").click(function(){
var target = $(this);
var nid = getImageNodeID($(this).siblings(".image-span").text());
$.ajax({
    url: Drupal.settings.basePath + 'views/ajax',
    type: 'POST',
    dataType: 'json',
    data: 'view_name=fehrest&view_display_id=block_1&view_args='+nid,
    success: function(response) {
        var output = response[1].data;
        alert(output);  
        target.hide();
    },
    error: function(data) {
        alert('An error occured!');
    }
  });
}); 
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Vinu Sebastian
  • 568
  • 3
  • 16