0

I am trying to alert some data on the click on Create Quiz and it is working on the first page but on other pages, the alert is not working. I have attached the image for the table

enter image description here

I have tried to find on google, but nothing is available regarding this in datatables.

HTML CODE:

<table id="post-table" class="table display-table table-striped table-
bordered" width="100%" cellspacing="0">
<thead>
<tr>
  <th>Class</th>
  <th>Published</th>
  <th>Quiz</th>
  <th>Create Quiz</th>
  <th>View Quiz</th>
</tr>
</thead>
<tbody>
<?php foreach ($allposts as $allpost) { ?>
<tr>
  <td><?php echo $grades->getGradename($allpost['grade_id']); ?></td>
  <td><?php echo $others->milliToTime($allpost['created_at']); ?></td>
  <td><?php echo $quiz->quizCount($allpost['post_id']); ?></td>
  <td>
    <a href="#" data-post-id='<?php echo $allpost['post_id']; ?>' class="create-quiz"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>
  </td>
  <td><a href="/admin/quiz/allquiz.php?post_id=<?php echo $allpost['post_id'] ?>"><i class="fa fa-eye" aria-hidden="true"></i></a></td>
</tr>
<?php } ?>
</tbody>
</table>

SCRIPT

$('#post-table').DataTable({
    "bSort": false
});

$('.create-quiz').click(function(e){
    e.preventDefault();
    alert("ss");
});
topper1309
  • 171
  • 1
  • 13

1 Answers1

2

Your problem is that on the page load your browser execute your javascript and associate your code to the buttons existing in the first page. but when you change page those elements that you see are new elements without any event listener attached to them.

The easiest solution is to listen events on a container element or even on the document. Something like:

$(document).on('click', '.create-quiz', function(){ console.log('here i am'); }

Should work

wezzy
  • 5,678
  • 3
  • 29
  • 42