CodeMoose
CodeMoose

Reputation: 3025

JQuery - output object as text?

So, I've got a widget that is looking to output live HTML source as text, as part of a review process. Is there a way that I can take a selected element, append it to a target div, and have it output as text instead of as a child in the DOM?

UPDATE: I'd like to avoid textareas if possible - they present a complication in my form setup, as well as a vulnerability for anyone with basic skill in firebug/inspectEl.

Upvotes: 0

Views: 1930

Answers (3)

Jehanzeb.Malik
Jehanzeb.Malik

Reputation: 3390

If you want to avoid textarea then the only other possible option I know is of <code> tag and <pre> tag will recognize white spaces so that code is properly indented. Try this fiddle.

jsfiddle

Upvotes: 1

DaveMongoose
DaveMongoose

Reputation: 905

You need to escape the html characters, so < becomes &lt; etc. - see answers here: Escaping HTML strings with jQuery

Upvotes: 2

Theo Kouzelis
Theo Kouzelis

Reputation: 3523

You could insert the code into a textarea tag and the html will be printed as text

 jQuery('#idOfTextArea').text(html);

Upvotes: 2

Related Questions