2

I want to create a new datatable but when I try to add jQuery "click()" function its not really working. For Example:

$('#dataTable').DataTable();
$('.example').on('click', function(){
    alert("This is not working");
})

This will not work. But if I create a drawcallback function:

$('#dataTable').dataTable({
    "drawCallback": function() {
        $('.example').on('click', function(){
            alert("This will work");
        })
    }
}); 

Its working perfectly. Where I make a mistake? How can I make functions work without calling drawcallback? Thank you! Have a nice day!

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
kviktor1230
  • 101
  • 7
  • Please provide a [mre]. – Unmitigated Dec 13 '21 at 19:26
  • 2
    Use [event delegation](http://jqfundamentals.com/chapter/events#event-delegation) since your `.example` is created dynamically : `$('#dataTable').on('click', '.example', () => alert("This is not working"))` Your current working example could set a handler multiple times – Ruan Mendes Dec 13 '21 at 19:28

0 Answers0