How to update a specific div on the page?

0

I would like that after $ajax has finished, update the div tag that has as #coments , because in this div it loads the data from the database.

HTML

<div id="coments">
    <!-- // Query dos comentários PAI -->
    <div class="you-coment-post">
        <div class="avatar">
            <img src="logo-face.png" alt="">
        </div>
        <div class="box-you-write-post-coment">
            <span class="title-you-post-coment">Ramon</span>
            <p class="you-text-post-coment">
                E aeeee             </p>
        </div>
    </div>         
    <!-- // Fim do Bloco -->
</div>

AJAX

function checkKeyPai() {
    var tamanho = $("#comentario-pai").val().length;
    if (window.event.keyCode == 13) {
        var textareaPai = $('#comentario-pai').val();
        var posicao = $('#comentario-pai').attr("data-posicao");
        var idUser = $('#comentario-pai').attr("data-idUser");
        var idPost = $('#comentario-pai').attr("data-idPost");
        if (tamanho < 10) {
            alert("Você precisa digitar pelo menos 10 caracteres");
        } else {
            if (idUser == "") {
                alert("Você precisa está logado!");
            } else {
                $.ajax({
                url: "/addComentario.php",
                type: "POST",
                data: "textareaPai=" + textareaPai + "&idUser=" + idUser + "&idPost=" + idPost + "&posicao=" + posicao + "",
                dataType: "html"

                }).done(function(resposta) {
                        alert(resposta);

                }).fail(function(jqXHR, textStatus ) {
                    alert("Request failed: " + textStatus);

                }).always(function() {      
                    console.log("completou");
                });

            }
        }   
    }
}
    
asked by anonymous 05.09.2017 / 20:27

1 answer

0

Is the ajax return already the html ready to insert inside the div? If yes, you only have to $('#coments').html(resposta) no .done

The .html method overrides all the html content of the selector you enter, in this case a div.

    
05.09.2017 / 22:38