Ajax jquery success scope

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.

Leave a Comment