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

84
Vistas
ajax reload is making problems with adding events

I have problem with following code. I have a div with id reloadDiv that is refreshing every 3 seconds using ajax. Content of the div is loaded using php, there is more content in this div but I didn't added it here.

Div is reloading normally. The problem is with input type button event with class replyComment, there are multiple buttons with this class here. When I add click event the event is working until first reload, as soon as div reloads event is not working any more.

Does someone know how to solve this problem?

<div id="reloadDiv">
     <?php foreach($comments as $c):?>
        <input type="button" value="Reply" class="replyComment" /> 
     <?php endforeach?>
</div>

js:

setInterval(function(){
    $("#reloadDiv").load(location.href + " #replyComment");
}, 3000);

$('.replyComment').click(function(){
    console.log("smth")
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

That happens because content is replaced, and the element with the event doesn't exists anymore, as well the event.

Try this:

$('#reloadDiv').on('click', '.replyComment', function() {

With that, the event is bound to the parent element and will work no matter how many times the content is recreated.

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