Web_Designer
Web_Designer

Reputation: 74520

Function to display html code rather than display elements in browser using Javascript

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?

Upvotes: 2

Views: 378

Answers (1)

Nathan Ostgard
Nathan Ostgard

Reputation: 8406

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);

Upvotes: 8

Related Questions