mkoistinen
mkoistinen

Reputation: 7773

Convert DOM elements to HTML Code and display in an on-page textarea?

I have the need to grab a few DOM Elements from an HTML page and emit their HTML (code) into a textarea on the same page. The purpose is to allow the viewer to copy and paste the code into their own pages.

Is this even possible?

I've tried something like (in jQuery):

$('textarea').val( $('.container').html() );

Where I've first cloned the elements I require into a div.container, but that just gives me something like object #<HTMLImageElement> in my textarea. What I was hoping to get was something like <img src="http://www.domain.com/images/file.png" alt="My Image"> according to the structure of the elements I cloned.

Any pointers appreciated! Thanks!

Upvotes: 2

Views: 1469

Answers (1)

Jason Foglia
Jason Foglia

Reputation: 2531

Try this:

$('textarea').val( $('.container').get(0).innerHTML );

Upvotes: 3

Related Questions