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

152
Visualizações
prevent keyup when input focus

I intent to trigger something with spacebar key, but spacebar should be prevent trigger when input is focus.

I'm trying with this way, but it doesnt work well is there any suggestion how to do it right.

$(document).ready(function(){
  $('input').on('blur', function(){
    $(document).on('keyup', function(e){
      if(e.keyCode == 32){
        alert("pressed!");
      }
    });
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label> Name</label>
  <input type="text" />
  <br />
  <label>Pass</label>
  <input type="text" />
</form>

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Detach the handler with .off() while input has focus:

$(function(){
    $(document).on('keyup', function(e) {
        if(e.keyCode == 32){
            alert("pressed!");
        }
    });
    $('input').on('focus', function() {
        $(document).off('keyup');
    });
    $('input').on('blur', function() {
        $(document).on('keyup', function(e) {
            if(e.keyCode == 32){
                alert("pressed!");
            }
        });
    });
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label> Name</label>
  <input type="text" />
  <br />
  <label>Pass</label>
  <input type="text" />
</form>

over 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