0

I have a piece of JavaScript that is supposed to update a in my HTML:

var StringContent = ({
    "a": 'Some String a',
    "b": 'Some string b',
    "c": 'Some string c',
});

Then I want each string a, b, c displayed on a new line via:

document.getElementById("overlaycontent").innerHTML = (
    StringContent.a + '\n' +
    StringContent.b + '\n' +
    StringContent.c,
    )

All I get at the moment is everything in a single line. How do I create a new line in my text without adding more divs? I also tried \r, but that also does not help. I looked at the docs, but it keeps saying to use \n.

3
  • 4
    In HTML, you should use <br> instead of \n. Commented May 12, 2014 at 15:20
  • Or use a class or block element and let CSS do the work. Commented May 12, 2014 at 15:22
  • The generated text will have new lines, however, under most situations, white space is collapsed with HTML. This is why you are seeing the recommendation to use <br>. You could also generate conteint within <pre> tags which do respect the white space. Commented May 12, 2014 at 15:27

4 Answers 4

5

You should replace \n by <br> since innerHTML takes an HTML string (in HTML, \n merges with adjacent spaces but does not produce a carriage return except for elements with the style white-space:pre-wrap as noted by MaxArt) :

document.getElementById("overlaycontent").innerHTML = (
    StringContent.a + '<br>' +
    StringContent.b + '<br>' +
    StringContent.c,
)
Sign up to request clarification or add additional context in comments.

2 Comments

I would say can rather than should
It's not that "\n means nothing", but rather whitespaces are collapsed to a single space, unless you set the CSS white-space property to pre or other variants.
2

CSS! white-space! pre-wrap! Learn about it!

<div style="white-space: pre-wrap">




                       SPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACE!

Newlines totally work now!
</div>

5 Comments

That's quite an enthusiatic endorsement of CSS.
@MaxArt Hehe... well, it's such a useful feature... People need to know!
This way you can even read the text back and do not have to replace <br> with a \n to get back to the original, bonus. :)
And several spaces in a row works too - great if you're trying to do any kind of indent for whatever reason, and for basic ASCII art!
Nope, let's choose the sucky way. :P
1

for innerHTML you need '<br />'

document.getElementById("overlaycontent").innerHTML = (
    StringContent.a + '<br />' +
    StringContent.b + '<br />' +
    StringContent.c
    )

but for an alert you can use : String.fromCharCode(10) instead of '\n'

alert(
    StringContent.a + String.fromCharCode(10) +
    StringContent.b + String.fromCharCode(10) +
    StringContent.c
    )

Comments

0

\n (or sometimes \r\n) will work when outputting to a text element such as <textarea> or to a .txt document, but <br> is required for HTML.

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.