Save text from one TEXTAREA with button and print to another div

Asked

Viewed 269 times

0

How I save a text from a Textarea in a variable with a click on a button, and then print that text by clicking another button on another textarea?

1 answer

3

good morning. I will explain the step by step according to your questions.

How to save a text from a Textarea to a variable?

Our input’s and button:

<input id="primeiroInput">
<button id="insertButton" onclick="myFunction()">Insere no outro Input</button>
<input id="segundoInput">

Using pure JS we can first have access to the first element Put and store in variable x with the following code snippet:

var x = document.getElementById("primeiroInput").value;

Second part of the question:

...and then print this text by clicking another button on another textarea?

With this we have stored in the variable x the value of your input now we need to define what the click event of our button will do:

function myFunction(){
var x = document.getElementById("primeiroInput").value;
document.getElementById('segundoInput').value = x;
};

Briefly: We created a function with name "myFunction" and inside it we have the part that we took the value of the "first Put" and passed this value to the "second Put". Note that when creating the button we insert in the event "onclick" the function "myFunction".

In this case we don’t need two buttons according to your question. In fact we can do even without buttons just using the input onchange event.

I hope I helped. Hugs

Browser other questions tagged

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