Benn
Benn

Reputation: 5023

How to change AJAX response HTML and then continue replacemant in jQuery?

I have an AJAX that gets new HTML and replaces the form data in response.html. There is one attribute that I need changed before the replacement.

Here is my try but I cant get it right:

success: function(response, status, xhr) {
    if (response.data.html !== null) {
        var newdata = response.data.html;
        if(firstload == 1){
            var newhtml  = $(newdata).find('.trigger').attr('data-firstload',0);
            // now I need to replace newdata
        }
        $(container).html(newdata); 
    }
}

Upvotes: 0

Views: 1054

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337570

You can create a jQuery object containing the new HTML, update it, then append it. Try this:

success: function(response, status, xhr) {
    if (response.data.html !== null) {
        var $newHtml = $(response.data.html);

        if (firstload == 1)
            $newHtml.find('.trigger').attr('data-firstload', 0);

        $(container).empty().append($newHtml);
    }
},

Upvotes: 1

Related Questions