1

How can I make a line break in a proper way in Javascript?

I was going through the w3schools site for a solution and they came up with this:

<p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello \ Dolly."; </script> 

I tried this code in their Tryit Editor v2.1 provided in their site but this code doesn't work properly there. In the output, everything's ok, except there is actually no line break between the strings 'Hello' and 'Dolly'.

I've also checked this code in my Netbeans 8.0.1 ide and encountered the exact same problem here as well.

I have no idea where the problem is. Or is this not the right way to make a line break? If so, what is the right way to make a line break? Is there a better approach? Please help.

My browser is Mozilla Firefox ver. 32.0.3

Edit - 1:

Thanks a lot to users @ahmadalbayati, @webeno and @TheProvost for providing me the solution. It has been fixed. However, I'd like to add that, apart from their solution, the following code:

<script> document.write("Hello <br/> Dolly."); </script> 

gives me the exact same output as well. The same, just instead of manipulating a specific html element, it's writing something to the whole html.

9
  • Possible Duplicate of stackoverflow.com/questions/22272623/… Commented Oct 31, 2014 at 5:47
  • you have to know what you want. add "\n" or "<br/>" Commented Oct 31, 2014 at 5:50
  • 2
    then do that: document.getElementById("demo").innerHTML = "Hello <br/> Dolly."; Commented Oct 31, 2014 at 5:53
  • 2
    This is the only and best way to do it. Commented Oct 31, 2014 at 5:57
  • 1
    w3schools seems pretty much misleading nowadays. Too bad. :( Commented Oct 31, 2014 at 6:03

2 Answers 2

5

"\" allows you to have multiple lines that the browser can understand for the script. But it wont make a new line for the output. Do something like this to create a new line in the output.

<script> document.getElementById("demo").innerHTML = "Hello <br/> Dolly."; </script> 
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry my friend, but your code doesn't give me the right output. It still has the problem of 'Hello' and 'Dolly' both being in the same line.
oh i forgot /n does not work for IE. <br> works though but webeno answered it first.
\n doesn't make a new line on the screen, it doesn't work for any browser. Thought it does make a linebreak in the codes.
@TheProvost the proper way of doing this, to my view, would have been if you added <br /> as an edit and either credited it to ahmadalbayati or myself...
@ahmadalbayati really? shoot probably getting my c# and javascript all messed up...lols tnx for the heads up bro..this is what i like about stack overflow. you always learn new things you thought you already knew :)
|
3

Purely going out from your example (doing this in html), the following should work:

document.getElementById("demo").innerHTML = "Hello <br /> Dolly.";
<p id="demo"></p>

EDIT: just mentioning @ahmadalbayati's name here who provided the answer first in a comment, though I have not seen that before posting this answer (probably didn't refresh the screen).

2 Comments

+1. Although originally answered by @ahmadalbayati a few seconds ago, but I'm accepting this as answer. Thanks dude. :)
i didn't see @ahmadalbayati's answer, just gave him credit now in an edit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.