yourfriendzak yourfriendzak - 3 months ago 18
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

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)
$("#TheDivIWantToChange").append(myString);

Javascript/JQuery HTML Encoding