Total Pageviews

2018/07/13

[JavaScript] How to set an array of data to textarea ?

Problem
I have a textarea as bellows:
   <textarea class='form-control' id='sentencesToLabeledData' name='sentencesToLabeledData' rows='10' cols='50'> </textarea>

How to set an array of data totextarea? And each element should add new line character

How-To
An array has a method to glue all elements together, Array.join. Without an argument, it would use a comma (,) as glue. To put every element on a new line, use the newline character (\n).
   // entity.sentences is an array of String
   if (entity.sentences != null && entity.sentences.length > 0){
       // apply Array.join to set new line character to each array element
       document.getElementById("sentencesToLabeledData").value = entity.sentences.join("\n");
   }



No comments: