jQuery does not return to HTML

0

I can not write the return inside the HTML element on success, it writes console.log , but not 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>");
            }
        });
    });
    
asked by anonymous 27.04.2016 / 14:14

1 answer

4

The "$ (this)" inside the Ajax success function is not a reference to your "comments" class object, but to the "jqXHR" object in Ajax. To get the correct reference, use the Ajax "context" option like this:

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