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

145
Visualizações
How to send KeyboardEvents to an input element

I am trying to send KeyboardEvents to an input element in a JSDOM based test. But for the sake of simplicity here is a web page example. According to every sample, blog post or tutorial I can find it looks like this should work. But it doesn't.

What else do I need?

<html>
<head>
  <title>Keyboard Event Conundrum</title>

  <script type="text/javascript">

window.onload = function() {
  var textField = document.getElementById("text-input");
  var button = document.getElementById("the-button");

  textField.value = "This works! ";

  button.addEventListener("click", function(event) {
    textField.value = "";
    textField.focus();
    // expected to add 'a' to the input's value, but doesn't
    var keyEvent = ekp = new KeyboardEvent("keypress", {
        altKey: false,
        bubbles: true,
        code: "KeyA",
        ctrlKey: false,
        key: "a",
        metaKey: false,
        shiftKey: false
    });
    textField.dispatchEvent(keyEvent);
  }, false);
}

  </script>

</head>
<body>
  <div>
    <input type="text" id="text-input" />
    <button id="the-button" type="button">send keyboard event</button>
  </div>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
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