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

238
Visualizações
Jquery keyup event is not triggered outside of document ready function

I wrote the keyup event in an external js file and it's referenced in the html file.

$('input[name=restaurant]').keyup(function(event){
    console.log("keyup");
});

The strange thing is that it won't activate unless I include it in document ready function.

$(document).ready(function(){
    $('input[name=restaurant]').keyup(function(event){
        console.log("keyup");
    });
});

The keyup event worked just fine outside of document ready function before. I don't know what the problem is now.

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

0

Because at the time your script ran, the element wasn't in the DOM yet. Thus, $('input[name=restaurant]') doesn't select any element, and no event listener is attached.

$(document).ready() waits for every element in the DOM to finish loading, then executes the callback.

See: Document: DOMContentLoaded event

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