I use this code to update a div using an AJAX request
var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("some_id").innerHTML += xmlhttp.responseText; } } xmlhttp.open("GET", "http://example.com/"); xmlhttp.setRequestHeader('Content-Type', 'utf8'); xmlhttp.send();
Everything works fine, the problem is that when there is a lot of content in the div with id some_id , I see that the content disappears and then appears after updating the AJAX request.
I think this is because
document.getElementById("some_id").innerHTML += xmlhttp.responseText;
Deletes and replaces the innerHTML div with the previous innerHTML plus new content, resulting in the behavior of previous content β blank β updated content .
Is there a way to add new content to a div instead of replacing all of it with new content?
javascript html
Backslash
source share