Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript read each line into array from a modal textarea

I have a modal

<div class="modal-body">
                    <textarea id="data" class="form-control" oninput="this.style.height = ''; this.style.height = this.scrollHeight + 3 +'px'"></textarea>                   
                </div>

and to save the user entered text from the textarea I have a save button and on click I want to save the text

$('#save').click(function () {
  var value = $("#data").val();
}

I would like the text to be stored in an array with each line an item in the array. So
text of the below would be an array of 4 items. Some line items will have a space between them but they need to be kept together as a single item in the array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ABC123
XYZ 444
12323321
OOOO XXXX

Setting var value to be var value = []; reads each letter into an individual array item so not what I need.

Thank you!

>Solution :

try this

$("#save").click(function () {
  const value = $("#data").val();
  const splitted = value.split("\n");
  console.log(splitted);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading