Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda