I have this simple JavaScript:
JavaScript
x
10
10
1
function property() {
2
var ua = document.getElementById('greenBack').innerHTML;
3
var finals;
4
finals = ua;
5
if (ua.indexOf('p')) {
6
finals += '<br>n Unknown Error';
7
}
8
return finals;
9
}
10
The problem is that I would like a newline to be shown when the function output is displayed in console.log()
without the <br>
tag (because <br>
displays on the console) , but also be able to write the text “Unknown Error” to a newline in html without using <br>
.
Is there any solution to display a newline in HTML and the console without n
or <br>
?
Advertisement
Answer
Just use n
for the console output. Then, when showing the text on a HTML page, you can either:
- replace
n
with<br>
- or wrap a
<pre>
tag around it which will respect white-space and newlines - or use CSS-style
white-space: pre-wrap;
on any other HTML element
See this jsFiddle
JavaScript
1
1
1
$('#test').text('Thisn isn antest');
JavaScript
1
2
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
<pre id="test"></pre>