John Smith John Smith - 10 months ago 41
jQuery Question

What is a better alternative to $("#myDiv").html(myResult); in this scenario?

I am currently using Ajax to update a feed of information. The Ajax should add to the current list of results rather than replace the existing results.

So far I have created the Ajax required to fetch the data from a database, but in the callback function I am using the following callback function

fetchPosts.onreadystatechange = function() {
if(fetchPosts.readyState === 4) {

Obviously using
in the callback function means that any prior results are overwritten on the page. How can I change this so that the results add to the current list of results?


Use append or prepend

$("#resultfeed").append(fetchPosts.responseText);  // Adds at the end
$("#resultfeed").prepend(fetchPosts.responseText); // Adds at the top