I created a table where I want to add one function if user click masterCheckBox to that select all checkbox or selectbox one-by-one then change the table>tr
background color from default to red. so that user can determine which table>tr
he/she is selected by checkbox same vice-versa, when checkbox not check tr
background becomes default as white and then perform ajax as in my code.
1> Finally I made all function but I not able to do change background color effect which tr
is selected.
2> Remove check from master checkbox after ajax success.
Note : I search on stackoverflow and google about this but I not get the satisfied answer and tried to do make by showing various post on stackoverflow, But I am not able to make this finally, Please do not mark this post as on hold and duplicate and other. Please help me.
In my case for selecting all checkbox the code is below.
Select All CheckBox
$("#myFunctionCheckAll").click(function() {
var checkedStatus = $(this).find('span').hasClass('checked');
$("#mailBody tr .chChildren input:checkbox").each(function() {
this.checked = checkedStatus;
if (checkedStatus == this.checked) {
$(this).closest('.checker > span').removeClass('checked');
}
if (this.checked) {
$(this).closest('.checker > span').addClass('checked');
}
});
});
CSS
.removeRow {
background-color: #c2dbff;
color:#FFFFFF;
}
$('#btn_delete').click(function() {
var id = [];
$(':checkbox:checked').each(function(i) {
id[i] = $(this).val();
//Issue Come
if($(this).is(':checked'))
{
$(this).closest('tr').addClass('removeRow');
}
else
{
$(this).closest('tr').removeClass('removeRow');
}
//Issue end
});
if (id.length === 0) {
alert("Please Select atleast one checkbox");
} else {
if (confirm("Are you sure you want to delete this?")) {
$.ajax({
url: 'deletetr.php',
method: 'POST',
data: {
id: id
},
success: function() {
for (var i = 0; i < id.length; i++) {
$('tr#' + id[i] + '').css('background-color', '#f5f5f5');
$('tr#' + id[i] + '').fadeOut('normal');
}
}
});
} else {
return false;
}
}
});
<a id="myFunctionCheckAll" title="selectAllCheckboxOnce"><span class="option-link square"><input type="checkbox"></span></a>
<a id="btn_delete"><span class="option-link trash"><i class="fa fa-trash fa-lg" aria-hidden="true"></i></span>Delete</a>
<table width="100%" cellpadding="2" cellspacing="0" border="0" class="th" id="mailBody">
<tr class="message-row" id="3205">
<td width="1%" class="chChildren">
<input type="checkbox" name="name[]" class="action" value="3205">
<img style="width: 40px; height: 40px" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ6Mwikrsp1V3kTV0chB1-B5cjYfabyyxKgJthlsrwW2fcOJOIw" alt="username" class="class" />
</td>
<td width="25%">
<span class="snds">
<span class='credentials'>Name</span> </span>
<span class="label grey-label label-right">Label</span>
</td>
<td width="73%">
<a data-urlajaxMail="#" class="messagedetails">
<span class="ts">406924241</span>
<font color="#7777CC">
<span class="elps">773980766</span>
</font>
</a>
</td>
<td width="1%" nowrap="">
7 mins ago </td>
</tr>
<tr class="message-row" id="3205">
<td width="1%" class="chChildren">
<input type="checkbox" name="name[]" class="action" value="3205">
<img style="width: 40px; height: 40px" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ6Mwikrsp1V3kTV0chB1-B5cjYfabyyxKgJthlsrwW2fcOJOIw" alt="username" class="class" />
</td>
<td width="25%">
<span class="snds">
<span class='credentials'>Name</span> </span>
<span class="label grey-label label-right">Label</span>
</td>
<td width="73%">
<a data-urlajaxMail="#" class="messagedetails">
<span class="ts">406924241</span>
<font color="#7777CC">
<span class="elps">773980766</span>
</font>
</a>
</td>
<td width="1%" nowrap="">
7 mins ago </td>
</tr>
</table>