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

206
Visualizações
How to catch keyboard input with jQuery .keyup() function

Working on a simple hangman game, and I'm trying to catch the user input with the keyup(), but when I log it to the console I'm realizing something isn't working right... this is my code:

$(document).keyup(function(e) {

    userInput = e.value;
    console.log(userInput);

});

I also tried doing this, but it isn't working either.

$(document).keyup(function(e) {

    userInput = $(this).val();
    console.log(userInput);

});

oh, by the way, userInput is a global variable.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to get the value from target property of the event. Try the following way:

$('#txtInput').on('keyup', function(e) {

    var userInput = e.target.value;
    console.log(userInput);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Input: <input type='text' id="txtInput"/>

about 4 years ago · Santiago Trujillo Relatório

0

$( "#whichkey" ).on( "keydown", function( event ) {
  $( "#log" ).html( event.type + ": " +  event.which );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="whichkey" placeholder="type something">
<div id="log"></div>

Use event.which

Description: For key or mouse events, this property indicates the specific key or button that was pressed.

about 4 years ago · Santiago Trujillo 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