I would like to change the content inside <div id="homePagePosts"> to be the new data from the server. The new data is in HTML format rendered by the server. However, the old HTML still stays the same after the AJAX data has been received by the browser.
When I console.log as shown below, I can see the rendered new HTML data is shown in the console. I have placed both Javascript and JQuery file at the bottom of the <body> but still doesn't work. I'm not too sure where it goes wrong here.
HTML
<div id="homePagePosts">
<% include ../ajaxSnippets/mainHome %>
</div>
Javascript
$.ajax({
url: "/main/home",
type: "POST",
contentType: "application/json",
data: JSON.stringify(sendData)
}).done(function(result){
//This shows up in the console
console.log(result)
updateMainPagePosts(result);
}).fail(function(err){
console.log(err);
});
var updateMainPagePosts = function(posts){
document.getElementById("homePagePosts").innerHTML = posts;
};
console.log(results), look atresults, step intoupdateMainPagePosts, look at what happens, etc.) Are you sure you don't have two elements with thatid? (Only the first would be updated.) Fundamentally, if you have an element withid="homePagePosts"in your document, the codedocument.getElementById("homePagePosts").innerHTML = posts;(withpostsbeing a parameter as shown) will update it.resultlogged with the expected content (so the ajax call works). I'm afraid we can't help you further as the code from that point (callingupdateMainPagePostsand the code within it) is correct. Debugging is the only way you'll find out what's happening. (It's also the first thing to do anyway. :-) ) Good luck!