Reputation: 29
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
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
Reputation: 202
Use $("#chat").append(data);
instead. Right now you're using .html()
which completely replaces the content of $(#chat)
.
Upvotes: 2