Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

86
Visualizações
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.

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!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

try this

$("#save").click(function () {
  const value = $("#data").val();
  const splitted = value.split("\n");
  console.log(splitted);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

HTML:

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

js:

const textArea = document.querySelector("#data");
const saveBtn = document.querySelector("#save-btn");

arr = [];
const saveData = function(){
  arr = textArea.value.split(/\r?\n/);
  console.log(arr);
}

saveBtn.addEventListener('click', saveData);

example: https://codepen.io/huskyslava/pen/PoORBVN?editors=1111

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda