Home > Back-end >  How to mirror an input field to another input field?
How to mirror an input field to another input field?

Time:11-04

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.

  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?

CodePudding user response:

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" />

  • Related