Reputation: 399
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
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