Sunghun Kwak Sunghun Kwak - 4 months ago 7
HTML Question

I can't use '\n" in javascript, I don't know why it isn't work

As I know, writing a new line is "\n", so I tried many times but it wasn't working. This is my source code and screen shot of result



var ary3 = new Array('seven','eight', 'nine');
for (var i =0; i<ary3.length ; i++){
document.getElementById('demo3').innerHTML += i+"'\nth element\n[enter image description here][1] : " + ary3[i]+"\n";
}

<h1>Show me the array object's entry</h1>
<div id = 'demo3'></div>
<br>




Answer

The problem is that the newline from JS will be rendered as plain space. HTML is responsible for new line showing, but HTML will not pay attention to simple new line in text. You can check your HTML using developer's tools. You will see that JS made new lines: derveloper tools

To make new line work, you should add <br /> tag

var ary3 = new Array('seven','eight', 'nine');
for (var i =0; i<ary3.length ; i++){
    document.getElementById('demo3').innerHTML += i+"'<br/>\nth element<br/>\n[enter image description here][1] : " + ary3[i]+"<br/>\n";
}
<h1>Show me the array object's entry</h1>
<div id = 'demo3'></div>
<br>

Comments