Saswat Saswat - 5 months ago 12
jQuery Question

Can't apply for on click function on each of the checkboxes

I want to apply for onclick function on each of the checkbox that is being added in a datatable row.

Here's the image:

enter image description here

Now the issue is that I am writing a code like this

var tablei = $('#domains_list').DataTable();
$('#domains_list').find("input[name='chk[]']:checked").each(function()
{
$(this).on('click',function ()
{
// make a class change to the parent tr having the checkbox;
} );
}
});


But the problem is, I can't write the rest of the code.

Answer

You don't need each to bind event. Also you should check checked in the click handler

Use

$('#domains_list').find("input[name='chk[]']").on('click', function() {
    //Perform your operation
    if (this.checked) {
        $(this).closest('tr').addClass('smclass');
    } else {
        $(this).closest('tr').removeClass('smclass');
    }
});

OR

$('#domains_list').find("input[name='chk[]']").on('click', function() {
    //Perform your operation
    $(this).closest('tr').toggleClass('smclass', this.checked);
});
Comments