boomcode
boomcode

Reputation: 399

Prevent striping of HTML tags

I am making an HTML editor in my application, which takes the HTML content from the database and copies it in an content editable DIV. But after making the changes, it strips the <html>, <head> and <body> tags, but keeps the body content.

How can I resolve the issue?

 document.getElementById('editHtmlView').innerHTML = document.getElementById('content').value;

where #content has the content fetched from database.

<div id="editHtmlView" contenteditable="true"></div>

Upvotes: 1

Views: 466

Answers (1)

Steve
Steve

Reputation: 10866

If you want the tags to show up as text, try changing .innerHTML to .textContent

.innerHTML tries to change the actual HTML in the div, which causes the browser to reject tags that don't make sense (like <html> tags inside a div). Setting content via .textContent preserves tags as text, and is more secure in general too.

Upvotes: 1

Related Questions