- 3

\n

why doesn't line break work?!

3rd Aug 2020, 4:51 PM
Ovidiu CATRINA
Ovidiu CATRINA - avatar
10 Answers
+ 7
May be you can present us a code snippet that shows this issue? Thanks!
3rd Aug 2020, 4:56 PM
Lothar
Lothar - avatar
+ 5
Elaborate your question and mention the language in tags
3rd Aug 2020, 4:54 PM
v@msišŸ˜‰
v@msišŸ˜‰ - avatar
+ 4
Ovidiu CATRINA Because document.write writes a string as HTML and HTML don't understand \n or it has a certain rule to change multiple chained whitespaces into 1 space. When you want to display newline in HTML you may use <br />: document.write("Hello World! <br /> next line");
3rd Aug 2020, 5:18 PM
Seb TheS
Seb TheS - avatar
+ 3
Context please šŸ˜³
3rd Aug 2020, 4:52 PM
Sandra Meyer
Sandra Meyer - avatar
+ 2
Use <br> instead of \n in document.write
3rd Aug 2020, 5:16 PM
v@msišŸ˜‰
v@msišŸ˜‰ - avatar
+ 2
I think it don't work if we write with text that will be part of document . I have tried it in innerHTML and document.write it didn't woork But works well in alert and console.
3rd Aug 2020, 5:20 PM
Divya Mohan
Divya Mohan - avatar
+ 2
To create a line break in JavaScript, use ā€œ<br>ā€. With this, we can add more than one line break also. <html> Ā  Ā <body> Ā  Ā  Ā  <script> Ā  Ā  Ā  Ā  Ā <!-- Ā  Ā  Ā  Ā  Ā  Ā  document.write("Hello World!"); Ā  Ā  Ā  Ā  Ā  Ā  document.write("<br>"); Ā  Ā  Ā  Ā  Ā  Ā  document.write("Demo<br><br>Text!"); Ā  Ā  Ā  Ā  Ā  //--> Ā  Ā  Ā  </script> Ā  Ā </body> </html>
4th Aug 2020, 5:11 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
code: document.write("Hello World! \n next line"); output: Hello World! next line
3rd Aug 2020, 5:10 PM
Ovidiu CATRINA
Ovidiu CATRINA - avatar
+ 1
Seb TheS, correct!
3rd Aug 2020, 5:28 PM
Ovidiu CATRINA
Ovidiu CATRINA - avatar
+ 1
Try <br/>
3rd Aug 2020, 7:51 PM
PandašŸ¼šŸ¼šŸ¼
PandašŸ¼šŸ¼šŸ¼ - avatar