user4913383 user4913383 - 4 years ago 109
Ajax Question

Does jquery append function avoid duplicating content somehow?

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?

Answer Source

that's because you do this:

$(".text").text("");

When you do that it empties the div every time you click the button.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download