How to access the textarea, grab its value and display it?

Asked

Viewed 178 times

1

I’m trying to create a notepad, and it stores the data in Localstorage, however, I cannot access the value that would be typed by the user, only what is already between the Textarea tag. I would like to take the value of the textarea, when the user type it, and display it somehow.


> HTML

<textarea id="txtar">
o console só consegue me retornar o valor escrito no documento, quando eu preciso ter acesso ao valor digitado no campo do TextArea
</textarea>


>JAVASCRIPT

console.log( 
  document.getElementById('txtar').value
  )
  • You can explain better what you want to happen and what’s happening?

1 answer

4


Help yourself, my brother.

let textarea = document.querySelector("#txtar");
let botao = document.querySelector("#botao");

botao.onclick = function() {
  console.log(textarea.value);
}
<textarea id="txtar">Texto digitado dentro do textarea</textarea>
<br>
<button id="botao">Exibir Texto</button>

What you type in the textarea is the value (value) of your field. So when you display the "textarea.value", you are displaying what you typed in the textarea (the value that your textarea now has is what you typed in).

You type in the textarea field and capture its value is the same as if you leave the textarea thus:

<textarea>Texto dentro do textarea e que é o seu value</textarea>

If you capture the value of that textarea above and display it, what will be displayed is the text that is within the textarea.

Note the example I put in the answer that if you just click the first button, the text (which becomes the value) already in the textarea will be displayed. Now if you delete that text and type something else, it will display what you typed (the new value).

  • let me see if I understand, to get the updated value of the text area, is it necessary for example the Function that the button calls p/ get the correct updated value? but I can automate it somehow? some kind of loop, to be something automatic

  • In fact you get the updated value when the "onclick" event occurs. That is, when you "click" on the button with the id="button", then a function is called that displays, through the console.log, the value that is in the textarea. I didn’t understand the "automate" part. It can explain better what would happen in this automation ?

  • yes, so, kind of use the Timeout of for example, 1 second to 1 second p/ go pick up the txtar.value and go playing in Localstorage, to go saving alone. MASSS, young man, your explanation has already made me understand all the necessary kkkk... very obg by the engagement.

  • You can pick up perfectly using the setTimeOut or setInterval. But then you would have to do as your need.

Browser other questions tagged

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