0

I'm using Jquery V1.11.1 and I want to replace a cell in a HTML table after a Jquery AJAX request. I want the result in the cell.

    $(".permission").click(function(e) {
    var acoPath = $(this).siblings('th').text();
    var rowsLeft = $(this).prevUntil("th").length;
    var aroGroup = $('.aro').eq(rowsLeft).text();

    $.ajax({
        url: "/permissions/update",
        type: "POST",
        data: { aco : acoPath, aro : aroGroup },
        cache: false,
        dataType: "HTML",
        success: function (html) {
            $(this).html(html);
        }
        });
    });
});

When I click on the text in the desired cell, nothing is replaced. How can i fix this?

2 Answers 2

5

You need to set the context in ajax call to current object. You can use context option from ajax to do that:

context: this,

Modified ajax call:

$.ajax({
    url: "/permissions/update",
    type: "POST",
    context: this,
    data: { aco : acoPath, aro : aroGroup },
    cache: false,
    dataType: "HTML",
    success: function (html) {
        $(this).html(html);
    }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Your context has changed. in the 'click' function this referce to the button. But in the callback function the context has changed. You should save the context to a variable first (var self).

$(".permission").click(function(e) {
var acoPath = $(this).siblings('th').text();
var rowsLeft = $(this).prevUntil("th").length;
var aroGroup = $('.aro').eq(rowsLeft).text();
var self = this;

$.ajax({
    url: "/permissions/update",
    type: "POST",
    data: { aco : acoPath, aro : aroGroup },
    cache: false,
    dataType: "HTML",
    success: function (html) {
        self.html(html);
    }
    });
});

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.