Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!