Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax jquery success scope

I have this ajax call to a doop.php.

    function doop(){         var old = $(this).siblings('.old').html();         var new = $(this).siblings('.new').val();          $.ajax({             url: 'doop.php',             type: 'POST',             data: 'before=' + old + '&after=' + new,             success: function(resp) {                 if(resp == 1) {                     $(this).siblings('.old').html(new);                 }             }         });          return false;     } 

My problem is that the $(this).siblings('.old').html(new); line isn't doing what it's supposed to do.

thanks.. all helpful comments/answers are voted up.

Update: it appears that half of the problem was the scope (thanks for the answers that helped me clarify that), but the other half is that I'm trying to use ajax in a synchronous manner. I've created a new post

like image 571
Chris Avatar asked Oct 15 '09 03:10

Chris


People also ask

Is ajax successful deprecated?

ajax function is deprecated.

What does success mean in ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

Is ajax better than jQuery?

jQuery uses ajax for many of its functions, but it nothing else than a library that provides easier functionality. With jQuery you dont have to think about creating xml objects ect ect, everything is done for you, but with straight up javascript ajax you need to program every single step of the ajax call.


1 Answers

You should use the context setting as in http://api.jquery.com/jQuery.ajax/

function doop(){     var old = $(this).siblings('.old').html();     var newValue = $(this).siblings('.new').val();      $.ajax({         url: 'doop.php',         type: 'POST',         context: this,         data: 'before=' + old + '&after=' + newValue,         success: function(resp) {             if(resp == 1) {                 $(this).siblings('.old').html(newValue);             }         }     });      return false; } 

"this" will be transfer to the success scope and will act as expected.

like image 184
Guillaume Bois Avatar answered Sep 20 '22 08:09

Guillaume Bois