Appending HTML-encoded string as HTML, not as text
43,449
// This is your string :)
var myString = "<div> hello </div>";
// 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);
HTML-encoding lost when attribute read from input field
Author by
zakdances
Updated on August 12, 2020Comments
-
zakdances over 3 years
I'm trying to append this string:
<div> hello </div>
as an HTML node, but instead of appending HTML it just appends the text:
<div> hello </div>
How do I make jQuery append this as an HTML node, not just text?
I'd like a solution that works both for nested divs AND text, if possible.
-
zakdances over 12 yearsWhat happens if there is complex nested divs inside the outer div instead of just the text "hello"?
-
zakdances over 12 yearsIs there a way to register the new html as a jquery node so it can be referenced as such (like with $('#newDiv' )?
-
normalUser over 9 yearsThanks!! That Saved Me! Btw I used to to append text dynamically in jquery cleditor premiumsoftware.net/cleditor
-
Andreas Covidiot over 2 yearscuriously this answer helped me find what I needed although it may not be the very correct answer to this specific question, coming here from googling. the difference of
x.text(...)
andx.html('...')
(wherex.append('...')
encodes likehtml(...)
) I see as quite important for my problem (appending without encoding to some<pre><code>...
node).