+ 1

java Script

'\n' is not working although i used it in different ways

13th Sep 2018, 4:05 PM
Motasem Gozlan
Motasem Gozlan - avatar
6 Respuestas
+ 9
Motasem Gozlan Short answer - Use <br> instead of \n when showing the output in HTML (i.e. when using document.write() or document.innerHTML) Long answer - When you write : document.write("Hello" + '\n\n' + "World"); The HTML source will be - Hello World ..and HTML removes the extra spaces (only keeps one space). So it shows only - Hello World When you write : document.write("Hello " + "<br>" + " World"); The source code is - Hello <br> World ..which HTML shows as Hello World Hope it clears! Feels free to ask if you've any more doubt
13th Sep 2018, 4:20 PM
Nikhil
Nikhil - avatar
+ 7
Please show us your code.
13th Sep 2018, 4:07 PM
Nikhil
Nikhil - avatar
+ 2
As far as I know, variable strings do not work with \n but with <br />, same as document.write&document.writeln alert and console will execute \n Look at the provided post, should explain it.👍 https://www.sololearn.com/discuss/1493807/?ref=app
13th Sep 2018, 4:18 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
+ 1
thank you nikhil its clear enough
13th Sep 2018, 4:24 PM
Motasem Gozlan
Motasem Gozlan - avatar
0
alert("Hello Motasem"); document.write("<h1>MGHZ</h1>"); var name = "Motasem"; var welcome = "\nWelcome " + name; var age = 20; var adulte = (age < 18)? "\ntoo young" : "\nold enough"; document.write(welcome + '\n'); document.write("\n you are " + adulte + '\n');
13th Sep 2018, 4:09 PM
Motasem Gozlan
Motasem Gozlan - avatar
0
thank you
13th Sep 2018, 4:20 PM
Motasem Gozlan
Motasem Gozlan - avatar