简体   繁体   中英

Cannot set multiline text in textarea

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

try this code

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

 }

its work with me

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>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM