Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline string in div using javascript

I have created a multiline string as follows"

var str1 = document.getElementById('text1');
var str2 = document.getElementById('text2');
var str = str1.value + "\n" + str2.value;

I now want to add this string to a div that I create in a popup window as follows:

myWindow=window.open('','Full_text','width=600,height=600');
myWindow.document.write('<div id="div1" style="position: absolute; left: 10px; width: 580px; top: 30px; height: 550px; overflow: auto;">' + str + '</div>');

But I do not get a multiline string in the div. How do I solve this problem?

like image 435
user828647 Avatar asked Jul 12 '12 09:07

user828647


People also ask

How do I create a multiline string in JavaScript?

There are three ways to create strings that span multiple lines: By using template literals. By using the + operator – the JavaScript concatenation operator. By using the \ operator – the JavaScript backslash operator and escape character.

What is the delimiter for multi line strings in JavaScript?

Method 1: Multiline-strings are created by using template literals. The strings are delimited using backticks, unlike normal single/double quotes delimiter.

How do you use multiline strings?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.


1 Answers

Line breaks are not rendered in HTML without this CSS property:

white-space: pre;

You can try to replace str to this:

var str = str1.value + "<br/>\n" + str2.value;
like image 125
Florent Avatar answered Nov 13 '22 01:11

Florent