I am using the following code in tables that can have checkboxes or radio buttons.
If the table contains checkboxes, I want to sync the click on the row with the checkbox and vice versa.
If the table contains radio buttons, I want to sync the click on the row with the radio button and also sync it with the other radio buttons since only one can be checked.
This code seems to work but I have merged several examples found on the forum so I wanted to know whether it's ok or if that can be improved?
Also:
What !==
means in Javascript?
Why $(this).closest(".active").removeClass("active");
does not work properly?
// Radio button in a table row
// Change the class of the row if selected
$(".radio-table-row").change(function() {
$(".table tr.active").removeClass("active"); //remove previous active class
//$(this).closest(".active").removeClass("active"); // Why this does not work?
$(this).closest("tr").addClass("active"); //add active to radio selected tr
});
// Checkbox in a table row
// Just add/remove classe based on the checked property
$(".checkbox-table-row").change(function() {
if( $(this).prop("checked") ) {
$(this).closest("tr").addClass("active");
}
else {
$(this).closest("tr").removeClass("active");
}
});
$('.product .table tr').click(function(event) {
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
}
if (event.target.type !== 'radio') {
$(':radio', this).trigger('click');
}
});
Thanks