In this example I am loading JSON data from reddit using ajax. As the response comes from server I append it to div with a class .text
.
<button type="button" class="btn">Click me!</button>
<div class="text">Replace me!!</div>
$(".btn").click(function () {
$.ajax({
url: "http://www.reddit.com/r/aww/search.json?q=puppy&restrict_sr=true",
method: "GET",
success: function (result) {
$(".text").text("");
for (var i = 0; i < result.data.children.length; i++) {
$(".text").append('<img src="' + result.data.children[i].data.thumbnail + '"/>');
}
}
});
});
This works. My question is why does append function not append same images again as I press button repeatedly? Does append function avoid duplicating content somehow?
$(".text").text("");
that you call every time on the success call. – LordNeo