Skip to content
Advertisement

How do I prevent HTML from ignoring the newline that comes from JavaScript?

I’d like to write my own hackertyper site that prints itself when pressing some keys.

But when I try the code below, it prints everything at the same line. I used template literals (``) and couldn’t make it. Then tried <br /> in the string but also not worked. How do I make it right?

Edit: I was using <p> tag in the HTML file, when I changed it to <pre> tag as Praveen said, it solved my problem. Thank you.

JavaScript
JavaScript

Advertisement

Answer

I guess if you’re trying to print it out, you need to print it out on a <pre> tag or an element with style white-space: pre or similar.

Here’s an example with <pre> tag:

JavaScript
JavaScript

Here’s the same with white-space: pre for <div>:

JavaScript
JavaScript
JavaScript

And, finally, just wanted to tell, this is a really good implementation of HackerTyper.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement