38

It seems that i cannot access $(this) inside jquery ajax success function. please see below code.

 $.ajax({
   type: 'post',
   url: '<?php echo site_url('user/accept_deny_friendship_request')?>',
   data: 'action='+$action+'&user_id='+$user_id,
   success: function(response){
     //cannot access $(this) here $(this).parent().remove();
   }
 });

6 Answers 6

66

What should $(this) be? If you have a reference to it outside that function, you can just store it into a variable.

$('#someLink').click(function() {
    var $t = $(this);
    $.ajax( ... , function() {
        $t.parent().remove();
    });
}
2
  • 1
    what if you can't add the var because it is wrapped in a function such as: $('.fileupload').fileupload({ dataType: 'json', start: {} ... etc
    – Alex
    Commented Feb 16, 2017 at 5:39
  • so it should be $('.fileupload')? If so, then: var $t = $('.fileupload').fileupload(...)
    – nickf
    Commented Feb 16, 2017 at 12:15
62

Check out the context option - works perfectly for me:

$.ajax({
    context: this,
    type: 'post',
    url: '<?php echo site_url('user/accept_deny_friendship_request')?>',
    data: 'action='+$action+'&user_id='+$user_id,
    success: function(response){
       //can access this now!
    }
});
1
  • I agree this is the right choice according to the documentation. In my experience I've not been able to use the context solution when using $.post instead of $.ajax. It produces an error: "Uncaught TypeError: Illegal invocation". I am running jQuery v1.11.3 and understand that the invocation error is related to the prototype function being called, but couldn't fix it even after finding the line that was blowing up. This seems to be one more difference between $.post and $.ajax. Using a variable, defined to have scope over the function, was the working solution in that particular situation. Commented Mar 18, 2016 at 4:26
10

If you want this to be this in the context of your ajax call, you can also use .bind() like the following:

$.ajax({
  url: 'some_url'
  success: function(data) {
    // do something 'this'
  }.bind(this)
})

It binds the value of this inside the success callback to this outside.

1
  • this is the only thing that worked for me. thank you.
    – Hikaros
    Commented Dec 15, 2021 at 23:30
4

Try calling $.proxy, to change the scope of this inside the function:

$.ajax({
    success: $.proxy(function(response) { $(this).parent().bla(); }, $(this));
});
0
1

Now, you can simply achieve it using ES6 arrow function. You can convert the anonymous function to arrow function expression as below:

 $.ajax({
   ..,
   success: (response) => {
     // access this outside of this function scope by using `this`
   }
 });

Make sure to use transpiler such as babel to provide support for older browsers.

0

I can't see $(this) referencing anything but easier way would be to give the element a class or id and reference that from jquery:

Instead of:

$(this).parent().remove();

You could do:

$('#element_id').parent().remove();

Note: Here I assume that you are dealing with one element/iteration.

1
  • I have not included the code $(this) is referencing to. but it would be some thing like below $('#element_id').click(function(){$.ajax({...})});
    – Yalamber
    Commented Apr 16, 2010 at 10:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.