Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  70] [ 1]  / answers: 1 / hits: 18177  / 7 Years ago, mon, june 12, 2017, 12:00:00

How do I insert a newline character in Javascript? The below code displays everything in one line even when using the newline character n.





<body>
<button onclick=myFunction()>Click me</button>
<div id=demo onclick=myFunction()></div>
<script>
function myFunction() {
document.getElementById(demo).innerHTML = line1nline2nline3nline4nline5nline6;
}
</script>
</body>




More From » html

 Answers
200

innerHTML




What happens if you put a new line character in HTML source code?





New
Line!





This is an HTML issue. Not a JavaScript issue.



If you want a new line in HTML, then you need to add appropriate elements.



The quick and dirty solution would be <br>, or wrapping the whole thing in a <pre>...</pre>, but the correct markup for your content might be a list or something else. Your example code is too generic to tell.


[#57488] Friday, June 9, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
estefanib

Total Points: 508
Total Questions: 104
Total Answers: 83

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;