Read a specific line in Javascript

Asked

Viewed 53 times

-4

Hello. I have an HTML code that receives a read information from a QR Code, which contains information such as Client, Danfe, Code and Quantity and displays it within a textarea. This reading is done through an application separately from the code. Follow the HTML code:

<div>
    <textarea type="text" id="dadosLidos" style="height: 250px; width: 700px;"></textarea>
</div>

The contents of this textarea are saved in a var in the Javascript file and printed in the console shortly after.

$scope.saveInfo = function() {
    var dados = document.getElementById("dadosLidos").value;  
    console.log(dados);
};

An example of information that the textarea receives is:

CLIENTE: FULANO DE TAL 
DANFE: 123456
CÓDIGO: 1234
QNT.: 2

What I need is to read only the content of the third line, ie, CODE: 1234. I’ll run a check against the code contained in each QR Code. How can I do to read or save in a variable only the contents of the third line, or the line CODE ?

1 answer

0


Simply make a split by line breaks \n.

You will get an array of all lines of text. The contents of the 3rd line will be at position 2:

const textoLido = `CLIENTE: FULANO DE TAL 
DANFE: 123456
CÓDIGO: 1234
QNT.: 2`;

vTexto = textoLido.split('\n');

linha3 = vTexto[2]
console.log(linha3);

Will return:

CÓDIGO: 1234

Browser other questions tagged

You are not signed in. Login or sign up in order to post.