Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .append() not rendering html entity

I have a variable with html in it like this:

var html = '<div>Hello, you&#39;re awesome!</div>';

I want to append it to an element $("body").append(html) for some reason it's not decoding the HTML Entity. I tried $("body").append($(html)); and that didn't work. My text is stuck inside the element and I can't individually put it together.

Is there any way to append html with a text-based entity inside it into another element, and have the html entity render on the page?

I've read a bunch of posts on stackoverflow reguarding html entities and it seems that none of them include the html & text within a variable like this.

like image 241
ThomasReggi Avatar asked Apr 05 '13 20:04

ThomasReggi


2 Answers

Try this:

var html = $('<div>Hello, you&#39;re awesome!</div>');
$("body").append(html)

Demo here

like image 183
palaѕн Avatar answered Oct 05 '22 06:10

palaѕн


It could be possible that your page did not finish loading while the jQuery code was trying to append the content. Try:

$(document).ready(function (){
  var html = '<div>Hello, you&#39;re awesome!</div>';
  $('body').append(html)
});

And I would suggest using single quotations only unless you need to use escaped characters.

like image 29
Edward Avatar answered Oct 05 '22 07:10

Edward