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

125
Visualizações
Why is my jquery executing on second click only?

I've written a jQuery function which updates a div upon clicking another div. However, it is only firing on the second click each time and I'm rather confused.

How can I fix this?

$(document).ready(function(){ 
  $(".block-swatch").click(function(){
    $("#refresh-feefo").load(window.location.href + " #refresh-feefo" );
  }); 
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should consider using .on as it allow to bind event from a parent and if your DOM element #refresh-feefo isn't ready, it will still work.

$(document).ready(function(){ 
  $("body").on('click', '.block-swatch', () => {
    $("#refresh-feefo").load(window.location.href + " #refresh-feefo" );
  }); 
});

I used body , but be careful:

Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $( "body" ).on( "click", "#commentForm .addNew", addComment ) use $( "#commentForm" ).on( "click", ".addNew", addComment ).

Read Event performance section here for more: https://api.jquery.com/on/

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