How to make a field type inside another? Javascript

Asked

Viewed 149 times

-3

I’ve been trying to make whatever I type appear inside another input, but I couldn’t. The point is I got this code here, it runs and it works right, but in my real code it doesn’t run.

window.oninput = function(event){
   
   var campo = event.target.id; // pega o id do campo que chamou o evento
   
   if(campo == "a"){
      document.querySelector('#a-1').value = document.querySelector('#a').value;
   }else if(campo == "a-1"){
      document.querySelector('#a').value = document.querySelector('#a-1').value;
   }
};
<h2>Este campo é diretamente automático</h2>
<input type="text" id="a"> oninput: <input type="text" id="a-1"><br><br>

1 answer

-1


Use the onkeyup event I believe will help you.

try like this

<input type="text" id="a" onkeyup="document.getElementById('a-1').value = this.value;"> oninput: <input type="text" id="a-1" onkeyup="document.getElementById('a').value = this.value;">

I hope I’ve helped.

  • It worked!!! Thank you my friend

  • With onkeypress the first typed in the field does not go to the other field. From then on the last typed will not, that’s if you type ABCD in the other field is like ABC

Browser other questions tagged

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