Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a line break on a Javascript concatenated string?

I'm sending variables to a text box as a concatenated string so I can include multiple variables in on getElementById call.

I need to specify a line break so the address is formatted properly.

document.getElementById("address_box").value =  (title + address + address2 + address3 + address4); 

I've already tried \n after the line break and after the variable. and tried changing the concatenation operator to +=.

Fixed: This problem was resolved using;

document.getElementById("address_box").value =  (title + "\n" + address + "\n" + address2 + "\n" +  address3 +  "\n" + address4); 

and changing the textbox from 'input type' to 'textarea'

like image 200
blarg Avatar asked Mar 12 '13 09:03

blarg


People also ask

How do you put a line break in a string?

Inserting a newline code \n , \r\n into a string will result in a line break at that location. On Unix, including Mac, \n (LF) is often used, and on Windows, \r\n (CR + LF) is often used as a newline code.

How do you split a line break?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings. Copied!

Which command is used for line break?

The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.

How do you insert a line break in node JS?

Try using \r\n instead of just \n. \n is fine; you're probably viewing the log file in notepad or something else that doesn't render non-Windows newlines. Try opening it in a different viewer/editor (e.g. Wordpad).


1 Answers

You can't have multiple lines in a text box, you need a textarea. Then it works with \n between the values.

like image 127
Guffa Avatar answered Oct 08 '22 20:10

Guffa