0

I have an array, each of its elements in a text line:

a = ['line1', 'line2', 'line3'];

I want to set the text lines into a textarea, displayed like so:

line1
line2
line3

I set the text with the following code:

myTxtArea.value = a.join('\n');

and it shows like this:

line1\nline2\nline3

What to do so that the \n's break lines instead of being displayed?

The textarea HTML:
<textarea autofocus id=laidTxt name=laidTxt class="laidTxt"
placeholder="paste PDF text here">KJHKH DSKJH ...
</textarea>

I see

Juan Lanus
  • 2,293
  • 23
  • 18

3 Answers3

0

try this code

for (let index = 0; index < a.length; index++) {
 myTxtArea.value += a[index]+'\n';

 }

its work with me

tawfik al
  • 36
  • 4
0

I dont know why it does not work for you but as it shows in the example below it works

a = ['line1', 'line2', 'line3'];

document.getElementById("laidTxt").value = a.join('\n');
<textarea autofocus id="laidTxt"></textarea>
Andam
  • 2,087
  • 1
  • 8
  • 21
0

To clear any doubts, the code I initially posted in the question is OK and works fine.

For the curious: I was applying a JSON.stringify() transformation to the joined lines just before setting them into the textarea.
Like so:
let txt = JSON.stringify( handleText( txtArea.value ) ); txtArea.value = txt;

Juan Lanus
  • 2,293
  • 23
  • 18