How to project the value that is inserted into an input into the content of another element?

Asked

Viewed 85 times

-1

I am trying to reproduce in real time what the person typed in a textarea for a paragraph, an example would be the very system of questions and answers of this site, I type something and automatically is reproduced below.

i tried several ways only when I type something ex: t', and type another key the JV it deletes t' and subtitui by the key I typed.

I’m doing the site with JV, HTML and CSS to know more about the JV only I got caught up in it!

<script>

    function tecla(){
        let paragrafo = document.getElementById('p')
        paragrafo.innerHTML = event.keyCode;
        console.log(event.keyCode);
    }
</script>

1 answer

1

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"

  1. 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

  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

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>

  • @Augustovasques ah yes! wonder! I made the proper corrections in the code snippets.. But, what was the real goal? or need? on the site says that "The purpose of these additional new rules is to make users whose keyboard layouts map Unicode characters to punctuation keys in a US keyboard layout able to use web applications that support Firefox only with ASCII-compatible keyboard layouts or only with a US keyboard layout. Otherwise, recently mapped values may conflict with other keys." in free translation.

  • @Augustovasques didn’t quite understand the purpose, but all right!

  • 2

    It is that there is not only a keyboard pattern. Here in Brazil the common is the QWERTY standard, but in other countries there are also QWERTZ, QWERTK,.... and the keycode cannot distinguish between these patterns with the possibility of a wrong keyboard mapping, therefore the depreciation.

  • @Augustovasques ah yes, so who is responsible for distinguishing this pattern is the right browser itself!? thanks for the definition..

  • 1

    Always concatenate at the end has other problems. For example, type "abc", then move the cursor back to the beginning of the line and type "d". The content of the textarea will be "dabc", but in the paragraph it will be "abcd". Perhaps it is simpler to take all the content of the textarea and play it at once in the paragraph. Remembering also what to use innerText instead of innerHTML can make a difference: https://jsfiddle.net/y84xhaju/

  • @hkotsubo yes, yes! as the author did not define that question in the question, i.e., based on his code paragrafo.innerHTML = event.keyCode; I believe his intention and desire is to get the key and store it in the <p tag> (I didn’t quite understand the purpose, but I’d do it this way that you mentioned too)

Show 1 more comment

Browser other questions tagged

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