0

I'm trying to break line in JavaScript, but it's not working, i tried:

document.write(a)\n;
document.write(b);

and

document.write(a);
"<br>"+document.write(b);

and

document.write(e);<br>
document.write(e);

But no one is working out :(

Can someone help me please?

  • 1
    `document.write(a + '
    ');` or `document.write('
    ' + b);`
    – Mamun Mar 16 '20 at 23:52
  • 1
    `document.write(a+ '
    ' + b)`, assuming *a* and *b* have been declared or created already.
    – RobG Mar 16 '20 at 23:52
  • document.write(a + "\n") – Mechanic Mar 16 '20 at 23:59
  • I have time and i'm bored so if you want, take your time write more of your code and explanation about your goal and i'll take a look at it. It seems to be your first post here, you'll get destroyed by comments but don't worry We all got destroyed at our first posts. – Antoine553 Mar 17 '20 at 00:07

3 Answers3

3
<script type="text/javascript">
    document.write(a);
    document.write("<br>");
    document.write(b);

    document.write(a + "<br>" + b);
</script>

How do I create a new line in Javascript?

Antoine553
  • 126
  • 1
  • 1
  • 10
1
document.write(a+"\n");or document.write(a+"<br>");
woofMaranon
  • 144
  • 3
  • 15
0

Using Document.Write isn't usually a good thing to use as its probably not doing what you think its doing, but anyway.

In your code, assuming a, b and e are string variables you need to put your line break inside the brackets.

document.write(a+"\n");
document.write(b+"<br/>");

Update

There is nothing wrong with document.write but if you don't understand what it does it can be confusing.

Whatever string you pass to the function is appended to the end of the document.

Example:

<html>
<body>
<h1>
<script> document.write("My Header!"); </script>
</h1>
</body>
</html>

You might think that this would write the text "My Header!" inside the h1 tag, but the result will actually be something like this

<html>
<body>
<h1>
<script> document.write("My Header!"); </script>
</h1>
</body>
</html>
My Header!

Document.write is great if you have an <iframe> or similar and you want to generate the whole document from scratch. Using it to change the current page doesn't work (very well). For changing the current page, you want to look into DOM Manipulation.

NickSlash
  • 4,758
  • 3
  • 21
  • 38