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'
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.
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!
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.
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).
You can't have multiple lines in a text box, you need a textarea. Then it works with \n
between the values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With