1

I am trying to hide a specific div content after jquery ajax success.

Live demo: http://jsfiddle.net/t922q/

Some of jquery code:

  $('.deletethisconent').click(function() {
    // $.ajax({   ....
         success: function(data){
         $(this).closest('.container').hide(); 
         $(".delete_status").html(data);

     });

How can I hide one the targeted div after ajax success? Thank you.

test
  • 53
  • 1
  • 10
  • possible duplicate of [$(this) inside of AJAX success not working](http://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working) and [jQuery ajax success doesn't work with $(this)?](http://stackoverflow.com/q/6889855/218196). – Felix Kling May 28 '13 at 15:11

3 Answers3

4

this refers to the jqXHR object which is the third argument in the success handler.

success:

Function( PlainObject data, String textStatus, jqXHR jqXHR )

You need to store a reference to the outer this in a variable if you want to reach it:

$('.deletethisconent').click(function() {

     var that = this;

     $.ajax({  
         url: 'echo.php', 
         success: function(data){
             $(that).closest('.container').hide(); 
             $(".delete_status").html(data);
         }
     });
});
Community
  • 1
  • 1
Johan
  • 35,120
  • 54
  • 178
  • 293
0

It seems like you want to hide the data, so:

$('.deletethisconent').click(function() {
    // $.ajax({   ....
    success: function(data){
         $(this).closest('.container').css('display', 'none'); 
         $(".delete_status").html(data);

 });
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123
0

Add a reference to this in your click function and then use it:

$('.deletethisconent').click(function() {
    var mainElement = this;

    $.ajax({
        success: function(data) {
            $(mainElement).closest('.container').hide(); 
        }
    });
});