0

Got a html page with a frequently changing part. That part is read by a script on the page:

<script>
  console.log('In HTML');
  fetch('changes/changes.html')
    .then(data => data.text())
    .then(html => document.getElementById('changes').innerHTML = html);
</script>

That works fine and makes publishing changes easy. However jQuery actions (in a separate script) on the read part don't work. The events are not triggered. How to fix that?

$(function () {
  console.log('In script');

   $('.change-card > .change-btn-action').on('click', function () {
     console.log('Click event');
   });
)};

The click event is never triggered. The log order is: In HTML, In script. Thanks for any help! Gerard

Gerard
  • 2,649
  • 1
  • 28
  • 46

2 Answers2

2

Attach an event handler on body tag or parent of the dynamic element in the page and pass the dynamic element as a selector.

 $('body').on('click', '.change-card > .change-btn-action', function () {
     console.log('Click event');
 });
 // or
 $('#changes').on('click', '.change-card > .change-btn-action', function () {
     console.log('Click event');
 });
Rohit Utekar
  • 828
  • 5
  • 10
0

Try this

$('body').on('click','.change-btn-action', function () {
     console.log('Click event');
});