JQuery onclick event not recognized in jQuery datatable from second page

I have a jQuery datatable and I want to delete a row when the delete link is clicked. It works fine for the first 10 lines, i.e. For the first page. When I go to any other pagination. This does not work. Here is my code:

$("#example tbody td.delete").click(function(event) { var row = $(this).closest("tr").get(0); oTable.fnDeleteRow( row ); }); 

All last td lines have a class of "delete".

What to do to work for all pages or for all posts?

+6
source share
3 answers

If you are using jQuery 1.7 or later, you need to use a real-time event handler, as subsequent pages are added dynamically.

 $('#example tbody td.delete').live('click', function(event) { var row = $(this).closest('tr').get(0); oTable.fnDeleteRow( row ); }); 

jQuery.live ()

EDIT:

It seems that people are still using this answer, so to update it with the latest best practices, DO NOT use , use .live (). Live was deprecated at 1.7 and removed at 1.9 . Use the .on () handler instead . This can handle delegated events by binding the event to the parent and using the actual element you want to target as an optional selection parameter. To use it in the above example, it would look like this:

 $('#example tbody').on('click', 'td.delete', function(event) { var row = $(this).closest('tr').get(0); oTable.fnDeleteRow( row ); }); 
+16
source

I stuck with the same thing when I try to bind an inline event that it works.

  onclick="$('#dataConfirmOK').attr('href',$(this).attr('url'))" 
0
source

If the current extension does not work, you can add an additional extension plugin. http://plugins.jquery.com/files/live-extension.js_4.txt

Preferably, if you are attaching a β€œClick” event to the following page elements at startup. Download the code below each time you load the page. and also define the function "click_function_to_call".

 <script type="application/javascript"> $("#example tbody td.delete").click(click_function_to_call); </script> 
-1
source

Source: https://habr.com/ru/post/898185/


All Articles