Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  33] [ 1]  / answers: 1 / hits: 42236  / 8 Years ago, fri, january 20, 2017, 12:00:00

I'm trying to render some HTML on the fly in my website without success. I've tried using jQuery's .html() function as below:



My html



<div id='open_ender_output'></div>


My JQuery



var openEnderContent = &lt;p&gt;&lt;span style=color: #ff0000;&gt;DDD&lt;/span&gt;!!!!!&lt;strong&gt;666666666666&lt;/strong&gt;&lt;/p&gt;
//openEnderContent comes from my backend
$('#open_ender_output').html(openEnderContent)


The result is



<p><span style=color: #ff0000;>DDD</span>!!!!!<strong>666666666666</strong></p>


Is there a way to make the browser render that result on the fly so it reflects the specific styles set on the text?


More From » jquery

 Answers
7

Decode the content by creating a temporary element.





var openEnderContent = '&lt;p&gt;&lt;span style=color: #ff0000;&gt;DDD&lt;/span&gt;!!!!!&lt;strong&gt;666666666666&lt;/strong&gt;&lt;/p&gt;';

$('#open_ender_output').html(
// create an element where the html content as the string
$('<div/>', {
html: openEnderContent
// get text content from element for decoded text
}).text()
)

<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<div id='open_ender_output'></div>








Or you need to use a string which contains unescaped symbols.





var openEnderContent = '<p><span style=color: #ff0000;>DDD</span>!!!!!<strong>666666666666</strong></p>';

$('#open_ender_output').append(openEnderContent);

<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<div id='open_ender_output'></div>




[#59275] Wednesday, January 18, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;