There is a web page, which contains 3 tables. "Total Opt Out" on page load is the sum of values from each table, all the rows, all the values.
When every record from the table is selected all the values from that row should be added together and subtracted from the total.
If the entire table is selected then all the values from all the rows should be added together and subtracted from "Total Opt Out" value.
With multiple tables, check and un-check records/ tables, finding it difficult. I have posted my code. Please provide me an idea on how to solve this?
Code as follows:
$(document).ready(function() {
$("#countUS").click(function() {
var table = document.getElementById("countryUS");
var val = table.rows[0].cells[0].children[0].checked;
var totalCountUSA = 0;
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].cells[0].children[0].checked = val;
}
if (val === true) {
$('#countryUS :checked').each(function() {
var totalAmount = $(this).parents('tr:eq(0)');
if ($(totalAmount).find('td:eq(2)').text() != '') {
totalCountUSA += Number($(totalAmount).find('td:eq(2)').text().replace(/[^0-9.-]+/g, "")) +
Number($(totalAmount).find('td:eq(3)').text().replace(/[^0-9.-]+/g, "")) +
Number($(totalAmount).find('td:eq(4)').text().replace(/[^0-9.-]+/g, ""));
}
$('#totalOptOut').val(totalCountUSA);
});
} else {
$('#totalOptOut').val("0.00");
}
});
});
function calculateTotal(countryCode) {
var theTable = 0;
var theTotal;
var countChecked = 0;
theTable = document.getElementById(countryCode);
for (x = 0; x < theTable.tBodies[0].rows.length; x++) {
for (y = 0; y < theTable.tBodies[0].rows[x].cells[0].children.length; y++) {
if (theTable.tBodies[0].rows[x].cells[0].children[y].tagName.toUpperCase() == "INPUT") {
if (theTable.tBodies[0].rows[x].cells[0].children[y].type.toUpperCase() == "CHECKBOX") {
if (theTable.tBodies[0].rows[x].cells[0].children[y].checked == false) {
if (theTable.tBodies[0].rows[x].cells[2].textContent) {
theTotal += (parseFloat(theTable.tBodies[0].rows[x].cells[2].textContent.toString().replace(/\$|\,/g, '')) +
parseFloat(theTable.tBodies[0].rows[x].cells[3].textContent.toString().replace(/\$|\,/g, '')) +
parseFloat(theTable.tBodies[0].rows[x].cells[4].textContent.toString().replace(/\$|\,/g, '')) +
parseFloat(theTable.tBodies[0].rows[x].cells[5].textContent.toString().replace(/\$|\,/g, '')));
} else {
theTotal += (parseFloat(theTable.tBodies[0].rows[x].cells[2].innerText.toString().replace(/\$|\,/g, '')) +
parseFloat(theTable.tBodies[0].rows[x].cells[3].innerText.toString().replace(/\$|\,/g, '')) +
parseFloat(theTable.tBodies[0].rows[x].cells[4].innerText.toString().replace(/\$|\,/g, '')) +
parseFloat(theTable.tBodies[0].rows[x].cells[5].innerText.toString().replace(/\$|\,/g, '')));
}
countChecked++;
}
break;
}
}
}
}
if (countChecked === theTable.tBodies[0].rows.length) {
if (countryCode === countryUS)
document.getElementById("countUS").checked = true;
else if (countryCode === countryCAN)
document.getElementById("countUS").checked = true;
} else {
if (countryCode === countryUS)
document.getElementById("countUS").checked = false;
else if (countryCode === countryCAN)
document.getElementById("countUS").checked = false;
}
$("totalOptOut").val(theTotal);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
Total Opt Out: <input type="text" id="totalOptOut" value="24750" disabled>
</p>
<table id="countryUS">
<thead>
<tr>
<th><input type="checkbox" id="countUS"> </th>
<th>State</th>
<th>Men</th>
<th>Women</th>
<th>Kids (Under age 12)</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td><input type="text" name="state" value="Texas" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td><input type="text" name="state" value="Arkansas" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td><input type="text" name="state" value="Arizona" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
</tbody>
</table>
<table id="countryCAN">
<thead>
<tr>
<th><input type="checkbox"> </th>
<th>State</th>
<th>Men</th>
<th>Women</th>
<th>Kids (Under age 12)</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td><input type="text" name="province" value="Ontario" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td><input type="text" name="province" value="Qubec" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td><input type="text" name="province" value="Alberta" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
</tbody>
</table>
<table id="countryMEX">
<thead>
<tr>
<th><input type="checkbox"> </th>
<th>State</th>
<th>Men</th>
<th>Women</th>
<th>Kids (Under age 12)</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td><input type="text" name="state" value="Jalisco" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="text" name="state" value="Puebla" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="text" name="state" value="Oaxaca" disabled></td>
<td><input type="text" name="men" value="1000" disabled></td>
<td><input type="text" name="women" value="1000" disabled></td>
<td><input type="text" name="kids" value="750" disabled></td>
</tr>
</tbody>
</table>