Duy Tran
Duy Tran

Reputation: 15

Line break in JavaScript?

I dont know why I cannot make a line break in my javascript. Here is the code:

document.write('It\'s ' + n + ',\n'+'SORRY, WE \'RE CLOSED');

Thanks,

Upvotes: 1

Views: 123

Answers (2)

Alohci
Alohci

Reputation: 82976

Works for me.

<span style="white-space:pre">
<script>
var n = 10;
document.write('It\'s ' + n + ',\n'+'SORRY, WE\'RE CLOSED');
</script>
</span>

Upvotes: 0

Alvin Pascoe
Alvin Pascoe

Reputation: 1209

That function will output HTML, so create line breaks by adding <br />, not \n.

document.write('It\'s ' + n + ',<br />SORRY, WE \'RE CLOSED');

http://jsfiddle.net/cuLdnjhu/

However, you shouldn't really be using document.write(): Why is document.write considered a "bad practice"?

A better option is document.body.innerHTML or document.createElement().

https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

https://developer.mozilla.org/en-US/docs/Web/API/Document

Upvotes: 2

Related Questions