The problem occurs due to the current operator used..
First of all I suggest you see the Allocation operators, because you are "assigning" the value and not "adding up"
- Assigning: in other words, you replace one value with another
x = y
(that is, if X is equal to 2 and Y is equal to 1, the value of X becomes 1)
var x = 2, y = 1;
console.log(x = y); // 1
- Adding: when we speak of numbers, here the value is summed
x += y
(X equal to 2 and Y equal to 1, the value of X becomes 3)
var x = 2, y = 1;
console.log(x += y); // 3
How the above problem involves String, that is, Text this operator passes to invite them, thus represented in javascript
var texto1 = "a", texto2 = "b", texto3 = "c";
var resultado = texto1 + texto2 + texto3;
console.log(resultado); // abc
note that the texts are stored in 3 different variables!
Therefore, to add without replacing the previous value/text just use the operator +=
var textarea = document.querySelector('textarea');
textarea.value += "21"; // value pois trata-se de uma entrada, diferente de um parágrafo <p>texto</p> (que entra o innerText())
<textarea>Minha idade é: </textarea>
see that the predefined text "My age is: " is maintained after adding the "21" and not replaced!
I made a small code where brings a possible solution based on your question!
Because it is missing the criteria of How to create a Minimum, Complete and Verifiable example it is extremely important to deal with this, to formulate a good question.
const textarea = document.querySelector('textarea'); // referencia da textarea
const p = document.querySelector('p'); // referencia do parágrafo
// dispara o evento ao ser pressionada as teclas
textarea.addEventListener('keypress', event => {
console.log(event.key); // imprime a letra, simbolo e/ou número correspondente a tecla pressionada dentro do textarea.
p.innerText += event.key;
});
<textarea></textarea>
<p></p>
You’re making
paragrafo.innerHTML = ...
, always overwriting the previous value. Instead, try to concatenate the contents.– Woss
Keyboardevent.keycode was depreciated and see innerHTML VS innerTEXT
– Augusto Vasques