Reputation: 1779
I have some code that calls in a new html file, to be added into a div. I'm wondering why the content in the div is replaced rather than just added in. Once I understand the "why" Id like to know how I would add in external markup into a div while preserving what was already in that div to begin with.
$.ajax({
url: 't3.html',
success: function(data) {
$('.ajax').html(data);
}
});
Upvotes: 1
Views: 117
Reputation: 466
Instead of:
$('.ajax').html(data);
use:
$('.ajax').append(data);
Upvotes: 1
Reputation: 4585
Because you are replacing the whole HTML of .ajax div with data. If you want to preserve the existing HTML of that control use the following $('.ajax').html($('.ajax').html() + data);d
Upvotes: 1
Reputation: 8476
try .append
$.ajax({
url: 't3.html',
success: function(data) {
$('.ajax').append(data);
}
});
Upvotes: 1