yourfriendzak yourfriendzak - 1 year ago 188
Javascript Question

JQuery: append html string as html

I'm trying to append this string:

<div> hello </div>

as an html node but instead of html it just appends the text

<div> hello </div>

How do I make JQuery append this as an html node, not just text?

EDIT: I'd like a solution that works for both for nested divs AND text, if possible

Answer Source

EDIT: Adding explaination...

// This is your string :)
var myString = "&lt;div&gt; hello &lt;/div&gt;";

// This is a way to "htmlDecode" your string... see link below for details.    
myString = $("<div />").html(myString).text();

// This is appending the html code to your div (which you don't have an ID for :P)

Javascript/JQuery HTML Encoding

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download