LALIT SINGH
LALIT SINGH

Reputation: 29

this code replacing the html not appending the html

suggest me how should i do to append the html not to replace html in django using ajax call this code replace li tag content replaces in html page but i want to append html on every ajax call

 {% load staticfiles %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="{% static 'chatbot/css/bootstrap.min.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'chatbot/css/Community-ChatComments.css' %}" type="text/css">
<link rel="stylesheet" href="{% static 'chatbot/css/styles.css' %}" type="text/css">
<script type="text/javascript">
$(function(){
$("#form1").on("submit",function(e){
    e.preventDefault();
    $.ajax({
    type:"POST",
    url:"./submission/",
    data:{
    "question":$("#input_ques").val() ,
    "csrfmiddlewaretoken" : $("input[name=csrfmiddlewaretoken]").val()        
    },
    success:sent,
    dataType:'html'

    });
  });
});
function sent(data,textStatus,jqXHR){
      $("#chat").html(data);

}

Upvotes: 0

Views: 52

Answers (3)

blurfus
blurfus

Reputation: 14031

Instead of $("#chat").html(data);

do

var existingData = $("#chat").text();
$("#chat").html(existingData + data);

or you could use jQuery's .appendTo() http://api.jquery.com/appendto/

$(data).appendTo("#chat");

or you could use jQuery's .append() http://api.jquery.com/append/

$("#chat").append(data);

This is assuming that data contains HTML elements (even in string format).

Upvotes: 2

Toby
Toby

Reputation: 202

Use $("#chat").append(data); instead. Right now you're using .html() which completely replaces the content of $(#chat).

Upvotes: 2

Satendra
Satendra

Reputation: 6865

Use .append() in place of .html()

$("#chat").append(data);

The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend()).

Upvotes: 2

Related Questions