SO I am trying to make a POS web based app in the billing part I am trying to add the total column only that are added by the JavaScript but it doesn't adds up
HTML: -
<table id="tbl">
<thead>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</thead>
</table>
I have created a button that dose a couple of things other then this, it should show the summed amount in the input field but this JS doesn't shows the summed amount: -
let subTotal = document.getElementById("tbl").getElementsByTagName("td");
let sum;
for (let i = 0; i < subTotal.length; i++) {
if (subTotal[i].id == "lstTtl") {
sum += isNaN(subTotal[i].innerHTML) ? 0 : parseInt(subTotal[i].innerHTML);
}
}
document.getElementById("sTotal").value = sum;
Trying to get a calculated amount from a table that is added dynamically.
isNaN will always return true for anything that isn't a number, which subTotal[i].innerHTML never is, therefore sum will always be +0.
if (subTotal[i].id == "lstTtl") {
const num = parseInt(subTotal[i].innerHTML);
if(!isNaN(num) {
sum += num;
}
}