Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
update text entry automatically on change

I have a simple HTML form with two text entries. I want to copy the text from the first entry field to the second one as soon as the text in the first field changes. Currently the changes are only applied, when I click the second entry. How can I fix this, so that the changes are visible without having to click the second entry?

<!DOCTYPE html>
<html>
    <body>
        <form onchange="myFunction()">
        <label for="field_1">field 1:</label>
        <input type="text" id="field_1" name="field_1"><br><br>
        <label for="field_2">field 2:</label>
        <input type="text" id="field_2" name="field_2"><br><br>
        </form>

        <script>
            function myFunction() {
                var x = document.getElementById("field_1").value;
                document.getElementById("field_2").value = "copy: " + x;
            }
        </script>
    </body>
</html>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Listen for the input event instead:

<!DOCTYPE html>
<html>
    <body>
        <form oninput="myFunction()">
        <label for="field_1">field 1:</label>
        <input type="text" id="field_1" name="field_1"><br><br>
        <label for="field_2">field 2:</label>
        <input type="text" id="field_2" name="field_2"><br><br>
        </form>

        <script>
            function myFunction() {
                var x = document.getElementById("field_1").value;
                document.getElementById("field_2").value = "copy: " + x;
            }
        </script>
    </body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Remove your myFunction() it will work. I have already tested it.

<!DOCTYPE html>
<html>
    <body>
        <form>
        <label for="field_1">field 1:</label>
        <input type="text" id="field_1" name="field_1"><br><br>
        <label for="field_2">field 2:</label>
        <input type="text" id="field_2" name="field_2"><br><br>
        </form>

        <script>
            document.getElementById('field_2').addEventListener("click", function() {
             this.value = document.getElementById('field_1').value;
             });

        </script>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda