Max Frai
Max Frai

Reputation: 64266

JQuery - ajax load

Some content...

Now I need to load some file into inner-block, but save it's old content.

$('#inner').load( 'pathToFile.html' );

Will replace old content of div.

Thanks.


So, as I understand my code should be:

old = $('#inner').html();
$('#inner').load( 'pathToFile.html' );
$('#inner').html( old + $('#inner').html() );

?

Upvotes: 0

Views: 685

Answers (4)

TigerTiger
TigerTiger

Reputation: 10806

Instead of the way you doing you need to look at these functions ..

append( content )   Returns: jQuery

Append content to the inside of every matched element.

appendTo( selector )    Returns: jQuery

Append all of the matched elements to another, specified, set of elements. As of jQuery 1.3.2, returns all of the inserted elements.

prepend( content )  Returns: jQuery

Prepend content to the inside of every matched element.

prependTo( selector )   Returns: jQuery

and yes @thenduks method is a better way.

Upvotes: 2

rfunduk
rfunduk

Reputation: 30432

I'd recommend against using stuff like 'load' and the other ajax helpers. They're just wrappers around $.ajax. Off the top of my head, maybe you want:

$.ajax( {
  url: 'pathToFile.html',
  type: 'get',
  success: function( r ) {
    $('#inner').append( r );
  }
} );

Upvotes: 5

jwl
jwl

Reputation: 10514

var originalContent=$('#inner').html();
$('#inner').load( 'pathToFile.html' );

is that what you are after?

Upvotes: 0

Mark Bell
Mark Bell

Reputation: 29735

var oldHtml = $('#inner').html();
$('#inner').load( 'pathToFile.html' );

Upvotes: 0

Related Questions