1

I want to use the $(this)-selector in the ajax success function to select the element .selectclass which was doubleclicked before. However, this doesn't work. Is it possible to select in the ajax success function?

jQuery:

$(document.body).on("dblclick",".selectclass",function(){
    bla = $(this).attr("class").slice(5); //works fine
    $.ajax({
        url: 'bla.php',
        type: 'post',
        dataType:'json',
        data: {
            'bla':bla
        },
        success: function(reply) {
            if((reply == "blabla")
            {
                $(this).siblings().find(".likes, .likes1").slideToggle('slow'); //not working 
            }
        }
    });
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Moritz
  • 745
  • 1
  • 10
  • 32

1 Answers1

3

You need to cache the variable.

$(document.body).on("dblclick", ".selectclass", function() {
  var $this = $(this);
  var bla = $this.attr("class").slice(5); //works fine
  $.ajax({
    url: 'bla.php',
    type: 'post',
    dataType: 'json',
    data: {
      'bla': bla
    },
    success: function(reply) {
      if (reply == "blabla") {
        $this.siblings().find(".likes, .likes1").slideToggle('slow'); // will work now 
      }
    }
  });
});

Please use var for local variables. Also you have a typo after if.

if((reply == "blabla") // Remove the double parenthesis.
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252