Cloning textarea values

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 value? I've tried document.getElementById("ta").value = document.getElementById("ta2").value , but it only works if you type in 1 and can not 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>
    
asked by anonymous 23.08.2018 / 07:47

1 answer

0

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

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>
    
23.08.2018 / 08:02