Replace a divs contents with jQuery

29 January 2009| No Comment| Print

I had a problem a while back where i needed to put the contents of a div into another div, the reason for this was because i couldn’t access the source code of the page to modify the div contents, as it was built using a CMS, so the only option was to use .append() in jQuery to do the job.

The code.


$(document).ready(function(){
var contentDiv = $('#contentDiv').html();
$('#Container').append(contentDiv);
$('#contentDiv').remove();
 });

You need to include the jQuery framework to get started, with the above configuration we’re taking the div #contentDiv and placing it into #Container, then we remove the original #contentDiv from the page.

Leave a comment...

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled site. To get your own globally-recognized-avatar, register at Gravatar.