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

204
Visualizações
change event in EventListener doesnt trigger

So I made a small version of my problem. Here I am adding text into an input element. But when it is added it doesnt trigger the event listener.

function testFunction() {
  document.getElementById("testInput").addEventListener("change", () => {
    console.log("hi");
  });
}

function testText() {
  document.getElementById("testInput").value = "Hello there";
}

testFunction();
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <title>Document</title>
  </head>
  <body>
    <script></script>
    <button onclick="testText()" style="height: 20px"></button>
    <input id="testInput" />
    <script src="script.js"></script>
  </body>
</html>

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

0

You need to create the event manually and use dispatchEvent() method to fire the event to the target element.

function testFunction() {
  document.getElementById("testInput").addEventListener("input", () => {
    console.log("hi");
  });
}

function testText() {
  document.getElementById("testInput").value = "Hello there";

  let event = new Event("input");
  document.getElementById("testInput").dispatchEvent(event);
}

testFunction();
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <title>Document</title>
  </head>
  <body>
    <script></script>
    <button onclick="testText()" style="height: 20px"></button>
    <input id="testInput" />
    <script src="script.js"></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