jQuery does not return to HTML

Asked

Viewed 70 times

0

I cannot write the return inside the HTML element in case of success, he writes the console.log, but not in html.

Code:

$(document).ready(function () 
{

    $('.comentarios').click(function () {
        $(this).siblings('.texto').prepend("<div>inicio da funcao<div>");
        $.ajax({
            method: 'get',
            url: url_destino,
            dataType: 'html',
            success: function (retorno) {
                console.log(retorno);
                 $(this).siblings('.texto').prepend("<div> info</div>");
            }
        });
    });
  • 1

    The first . prepend() can write? The DOM structure is correct so jquery can find the right place to write?

1 answer

4

The "$(this)" within the Ajax success function is not a reference to its "comment" class object, but rather to the Ajax "jqXHR" object. To get the correct reference, use the "context" option of Ajax, this way:

$.ajax({
    //...
    context: this,
    success: function(retorno) {
         //agora sim o $(this) está se referindo ao objeto de classe "comentarios"
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.