DOM Question

E

eggie5

I have the following function that build a div and adds it to the page.

function setDataJSON(req)
{
var data = eval('(' + req.responseText + ')');
console.log(data.news.length);
for (var i=0;i<data.news.length;i++)
{
var x = document.createElement('div');

var y = document.createElement('h3');
y.appendChild(document.createTextNode(data.news.story.headline));
x.appendChild(y);
var z = document.createElement('p');
z.className = 'moreInfo';
z.appendChild(document.createTextNode( data.news.story.summary ));
x.appendChild(z);
var a = document.createElement('img');
a.src = data.news.story.cover;
x.appendChild(a);
var b = document.createElement('p');
b.appendChild(document.createTextNode(data.news.story.body));
x.appendChild(b);
$('writeroot').appendChild(x);
}
}

A lot of my text in my textnodes: headline, summary and body have HTML
tags. However when i ad them to my page, they show ups literal text.
For example if body has the text of <b>bold text</b> it show up
literally like that instead of 'bold text' in bold. How can I have it
render the HTML?
 
M

Martin Honnen

A lot of my text in my textnodes: headline, summary and body have HTML
tags. However when i ad them to my page, they show ups literal text.
For example if body has the text of <b>bold text</b> it show up
literally like that instead of 'bold text' in bold. How can I have it
render the HTML?

Then don't do e.g.
pElement.appendChild(document.createTextNode(htmlSnippet));
instead set innerHTML
pElement.innerHTML = htmlSnippet;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top