Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to place html tags inside javascript string?

I have this code right here:

generation.innerHTML=
"<!DOCTYPE html>\n"+
"<html>\n"+
 "<head>\n"+
  "<style>\n"+
   cinput+
  "\n<\/style>"+
 "\n<\/head>"+
 "<body>\n"+
  hinput+
  "\n<script>\n"+
   jinput+
  "\n<\/script>\n"+
 "<\/body>"+
"<\/html>";

But the doctype, html, head and body tags dissapear when I console log it, but when viewing it as a P element, all of the tags go away and leave the hinput, cinput, jinput variables.

How do I make all of it stay when viewing as a P element?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

innerHTML tries to parse the data as HTML. Hence the name.

If you want to treat it as plain text, use textContent instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading