Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect checkbox select event in a table with jQuery DataTables

I am using jQuery DataTables for a table that loads data from the server using Ajax.

enter image description here

I have added a checkbox column and I need to fire an event every time I check a checkbox.

How can I do this?

like image 337
VAAA Avatar asked Oct 02 '15 16:10

VAAA


1 Answers

Since, I think, you are adding these elements dynamically in render function, you can use event delegation for the click event of the checkbox.

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

Ref: http://learn.jquery.com/events/event-delegation/

Code:

$(document).on('click', '.chkRow', function () {
    alert($(this).attr('value'));
});

I found a code for this purpose that add a checkbox on the row and on the click alert an attribute value.

Demo: https://jsfiddle.net/IrvinDominin/aqa61xdf/

like image 188
Irvin Dominin Avatar answered Oct 08 '22 11:10

Irvin Dominin