ndesign11
ndesign11

Reputation: 1779

Why does the jquery ajax call erase the current data?

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

Answers (3)

Sivaram Chintalapudi
Sivaram Chintalapudi

Reputation: 466

Instead of:

$('.ajax').html(data);

use:

$('.ajax').append(data);

Upvotes: 1

Tariqulazam
Tariqulazam

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

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

try .append

 $.ajax({
 url: 't3.html',
 success: function(data) {
   $('.ajax').append(data);
  }
 });

Upvotes: 1

Related Questions