2

I want to have the innerHTML of my div element to contain some HTML code, rather than displaying the expected result of that html as the browser normally would. How do I do this using Javascript?

Web_Designer
  • 68,768
  • 89
  • 200
  • 259

1 Answers1

8

With normal JavaScript:

var div = document.getElementById('foo');
while (div.firstChild) {
  div.removeChild(div.firstChild);
}
div.appendChild(document.createTextNode(html));

With jQuery:

$('#foo').text(html);
Nathan Ostgard
  • 7,808
  • 2
  • 25
  • 19