danjah
danjah

Reputation: 1246

Remove tag and content using Javascript

What I want is to get the content of a specific #id, remove a few tags and its contents, add some html before and after it, and then set that finished content as the body content.

I now have the following code, containing a mixture of Javascript and jQuery, although obviously not the right one - resulting in a [object Object]-message.

My code looks like this:

var printContents = jQuery("#"+id).clone();
var printContentsBefore = '<html><head></head><body><table><tr>';
var printContentsAfter = '</tr></table></body></html>';
var mainContents = printContents.find(".td_print").remove();

document.body.innerHTML = printContentsBefore + mainContents + printContentsAfter;

Any ideas of how to make this work?

Upvotes: 0

Views: 53

Answers (1)

collapsar
collapsar

Reputation: 17238

Your code does not convert the cloned jquery object to a string. Modify your code as follows:

document.body.innerHTML = printContentsBefore + mainContents.html() + printContentsAfter;

Beware that the .html method output will not include the html representation of the container element itself (ie. of the #id clone in your case).

Upvotes: 2

Related Questions