views:

118

answers:

4

Hello, I have a div:

<div id="inner">
    Some content...
</div>

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() );

?

A: 
var oldHtml = $('#inner').html();
$('#inner').load( 'pathToFile.html' );
Mark B
A: 
var originalContent=$('#inner').html();
$('#inner').load( 'pathToFile.html' );

is that what you are after?

larson4
+3  A: 

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 );
  }
} );
thenduks
+1 was about to write the same.
Konstantinos
What's wrong with using an ajax helper? I've used them myself with few problems.
Kai
Thanks, I'll remember this.
Ockonal
The helpers are simply wrappers on $.ajax and they're light on features. Of course $.ajax _is_ a helper, what I mean is the ones that wrap it. For example, you can't have a failure handler with $.get. It's very little extra code to just use $.ajax, you can omit features you don't need, and it's clearer for everyone reading it. All IMO, of course :)
thenduks
@Ockonal: np! :)
thenduks
+1  A: 

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.

Wbdvlpr