Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to mirror an input field to another input field?

I have an input field and multiple input fields, the original input field is the primary field that gets submitted.

So, I need to mirror one field to the main field.

I tried watching the keyup and change events but when the user pastes with the mouse it does not work, because the event is triggered before the user presses paste and not after it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. I am not sure that this is the best way to mirror two fields, so I am asking you if you have better methods of doing it
  2. Is there an event that can be triggered after the paste is pressed?

>Solution :

Instead of triggering on key/mousevents, you could try using an "input" event on the input element you would like to mirror.

const ogInput = document.getElementById("og")
const mirrorInput = document.getElementById("mirror")

ogInput.addEventListener("input", () => {
  mirrorInput.value = ogInput.value
})
<input type="text" id="og" />
<input type="text" id="mirror" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading