How to update a specific div on the page?

Asked

Viewed 58 times

0

I’d like that after the $ajax ended, updated the tag div that has as an id #coments, for in that div load 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");
                });

            }
        }   
    }
}
  • You know what the methods represent done, fail and always that you used?

  • The . done is always executed, while . Success is only called if success occurs.

  • And also that the done and fail methods are related to the return of the $.ajax. function only Always which have not understood much yet.

  • The method done only executed if the request was successful and it is in it that you should update the div.

  • But that you have my question how I make this update, and that I am very beginner yet.

  • It depends on what this "update" would be. Edit the question and add more details about it, explaining which is the div and what will be the new content.

Show 1 more comment

1 answer

0


The return of ajax is already html ready to insert inside the div? If yes you just make one $('#coments').html(resposta) no .done

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

  • With that you can help me thanks!

Browser other questions tagged

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