I have a text area that I want to pre-populate with some specific text that includes line breaks. I'm populating the area onLoad but I can't get the line breaks to work correctly. Any advice on how to do that? Thanks.
Asked
Active
Viewed 4.1k times
14
-
post code sample, data sample, environment and browser you're running. also give people credit for answers to your other questions. – lincolnk Jan 09 '12 at 19:26
6 Answers
18
You need to replace line breaks with newline characters: \n

Diodeus - James MacFarlane
- 112,730
- 33
- 157
- 176
8
Just use the newline character: \n
.
So, if you want the textarea to look like this:
This is line 1
This is line 2
You would use a string like this:
"This is line 1\nThis is line 2";
See a demo here: http://jsfiddle.net/MzmBd/

Joseph Silber
- 214,931
- 59
- 362
- 292
1
As stated multiple times, you need the \n
character.
see here: http://jsfiddle.net/XbALv/
document.getElementById("blah").value =
"This" + "\n" +
"is some" + "\n" +
"text" + "\n";

Joseph Marikle
- 76,418
- 17
- 112
- 129
0
i had tried many ways to break a textarea into an array. used this method, it might not be the best.
using .split('\n') does not remove the break and it will create whitespace or break when insert into database. therefore, i replace it with
textarea content:
12345
6789
a = a.replace(/\n|\r\n|\r/g, "<br>");
var stringArray = a.split('<br>');
for (var i = 0; i < stringArray.length; i++) {
var myString = stringArray[i];
myString = myString.replace('<br>', "");
response.write(myString);
}

Shawn
- 1
0
break a textarea into an array
textarea content: underscore_case first_name Some_Variable calculate_AGE delayed_departure
const variableCase = function (variable) {
const variableSplit = variable.split('\n');
const spaceRemoved = [];
for (const removeSpace of variableSplit) {
const RST = removeSpace.trim();
spaceRemoved.push(RST);
}
console.log(spaceRemoved); // output: ["underscore_case", "first_name", "Some_Variable", "calculate_AGE", "delayed_departure"]
};

Loot
- 3
- 1