1

I am trying to make new lines (in this single line of text) as you can see in my index.html file, but it is not working, any help? (\n is where the new line should start,
has not worked either.

index.html:

<html>
        <head>
                <title>test</title>
        </head>
<body bgcolor="black">
<font color="green">
<p id="terminal"></p>
<script>
var text = "this is a test\nthis should be on the next line";
var count = 0;
var speed = 50;
function Type() {
        if(count < text.length) {
                document.getElementById("terminal").innerHTML += text.charAt(count);
                count ++;
                setTimeout(Type, speed);
        }
}
Type();
</script>
7
  • 3
    <br> instead \n Commented Apr 3, 2018 at 19:33
  • White space is ignored when you set the innerHTML of an item. You can use <pre> to keep the white space inside of it's tag instead of <p>. Commented Apr 3, 2018 at 19:35
  • @LuisfelipeDejesusMunoz I tried telling about how that did not work but stackoverflow saw it as actual html code, when I use <br> it shows up: this is a test<br>this should be on the next line Commented Apr 3, 2018 at 19:36
  • 1
    You've just completely changed your question, to the point where the existing answers don't make sense. Please don't do that. Commented Apr 3, 2018 at 19:55
  • 2
    Because it shows a complete disregard for the people who have answered your original question? Please read How to Ask. If you have another question, ask a new question. Commented Apr 3, 2018 at 20:02

2 Answers 2

3

If you don't want to use <br /> you can easily use the <pre> tag. It's actually easier to use <pre> since you don't have to insert the <br /> at the right location in the DOM.

Taken from the docs.

The HTML pre element represents preformatted text which is to be presented exactly as written in the HTML file.

var text = "this is a test\nthis should be on the next line";
var count = 0;
var speed = 50;

function Type() {
    if(count < text.length) {
        document.getElementById("terminal").innerHTML += text.charAt(count);
        count ++;
        setTimeout(Type, speed);
    }
}
Type();
<html>
  <head>
    <title>test</title>
  </head>
  <body bgcolor="black">
    <font color="green" />
    <pre id="terminal"></pre>
  </body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

Substitute the new line \n with break <br>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.