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

76
Visualizações
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 Respostas
Responde à pergunta

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 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