Appending Html-encoded String As Html, Not As Text
I'm trying to append this string: <div> hello </div> as an HTML node, but instead of appending HTML it just appends the text:
hello
Solution 1:
// 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);
Solution 2:
You can create a new div with .createElement('div')
. Then simply change the new element's HTML.
$(document.createElement('div').html('<p>All new content.</p>'));
To attach the new element to the DOM either use element.append()
or .appendTo(element)
to append to your element of choice.
Solution 3:
Maybe this is a bit verbose, but this is usually how I handle stuff like that:
var div = $(document.createElement("div"));
div.text(" hello "); //Or div.html(" hello ") if need be...
$("#divtoappendto").append(div);
Post a Comment for "Appending Html-encoded String As Html, Not As Text"