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
ajax function is deprecated.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With