Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  172] [ 4]  / answers: 1 / hits: 43874  / 11 Years ago, fri, january 17, 2014, 12:00:00

I get JSON data with next code:



$.getJSON(
data.json,function foo(result) {
$.each(result[1].data.children.slice(0, 10),
function (i, post) {
$(#content).append( '<br> HTML <br>' + post.data.body_html );
}
)
}
)

<div id=content></div>


Some of strings included : &lt; and &gt; and this did not displaying as regular html <, >
Try to use .html() instead .append() did not work.



Here is live example http://jsfiddle.net/u6yUN/


More From » jquery

 Answers
2

Here is whet you were asking for: Complete JSFiddle Demo



var jsonRequestUrl = 'http://www.reddit.com/r/funny/comments/1v6rrq.json';
var decoder = $('<div />');
var decodedText = '';

$.getJSON(jsonRequestUrl, function foo(result) {
var elements = result[1].data.children.slice(0, 10);

$.each(elements, function (index, value) {
decoder.html(value.data.body_html);
decodedText += decoder.text();
});

$('#content').append(decodedText);
});





Edit: Keeping this here as a simpler example.



// Encoded html
var encoded = '&lt;div style=background:#FF0&gt;Hello World&lt;/div&gt;';

// Temp div to render html internally
var decode = $('<div />').html(encoded).text();

// Add rendered html to DOM
$('#output').append(decode);


DEMO


[#73103] Thursday, January 16, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;