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
  • 72,308
  • 93
  • 206
  • 262

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
  • 8,258
  • 2
  • 27
  • 19