Clone text area values

Asked

Viewed 41 times

1

How to clone the values of textarea so that when typing in textarea 1 the textarea 2 receives this value and when typing in textarea 2 the textarea 1 receives the amount?
I tried to document.getElementById("ta").value = document.getElementById("ta2").value, but it only works if you type in 1 and cannot type in 2

function preview() {
    var valor = document.getElementById("ta").value
    var valor2 = document.getElementById("ta2").value
    var preview = document.getElementById("preview")
    var preview2 = document.getElementById("preview2")
    
    preview.innerHTML = valor
    preview2.innerHTML = valor2
};
<textarea id="ta" oninput="preview()">Um</textarea>
<textarea id="ta2" oninput="preview()">Um</textarea>
<div id="preview"></div>
<div id="preview2"></div>

1 answer

0


It is necessary to assign the new value to the attribute value.

Example:

const ta = document.querySelector("#ta")
const ta2 = document.querySelector("#ta2")

function preview(input) {
    /* Captura o valor do campo digitado e adiciona nos campos `ta` e `ta2` */
    ta.value = ta2.value = input.value
};
<textarea id="ta" oninput="preview(this)">Um</textarea>
<textarea id="ta2" oninput="preview(this)">Um</textarea>

Browser other questions tagged

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