Hello I am trying to create a script that will calculate a total by addition or subtraction. The issue I am running into is the current JavaScript I am using that I found only works with the Starting Amount field and only one of the Bill Amount fields at a time. For example if I input 50 in the starting amount and 50 into the Bill Amount field it will calculate it. If I add a row and then input an amount it will still only adjust the amount based on whats input into one of the Bill Amount fields.
Not sure why but the code does not function on jsfiddle but you will be able to see the code in this jsFiddle.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Finance</title>
<meta charset="UTF-8">
<meta name="finance" content="width=device-width, initial-scale=1.0">
<style>
table {
padding-top: 15px;
}
td {
white-space: nowrap;
}
button {
cursor: pointer;
}
</style>
</head>
<body>
<h1>Financial Keeps</h1>
<p><b>Starting Amount: $ <input type="number" id="startAmt" name="startAmt" onkeyup="calc(this)"/></b></p>
<p>To subtract an amount place a minus (-) sign infront of the dollar amount.</p>
<button onclick="insertRow()" id="addRow" >Add Row</button>
<!--<button onclick="removeRow()" id="delRow" >Delete Row</button>-->
<table id="myTable">
<tr>
<th>Bill Info</th>
<th>Bill Amount</th>
<th>Date</th>
<th>Comment</th>
</tr>
<tr>
<tr>
<td><input type="text" id="billInfo"></td>
<td><input type="number" id="billAmt" name="number" onkeyup="calc(this)"></td>
<td><input type="date" id="date"></td>
<td><input type="text" id="commentBox"></td>
<!--<td><input style="cursor: pointer;" type="button" id="delBtn" value="Delete" onclick="removeRow(this)"></td>-->
</tr>
</table>
<input type="hidden" id="total" name="total" value="0" />
<p><b>Ending Amount: $ <span id="totalAmt">0</span></b></p>
<script type="text/javascript">
function insertRow() {
var x = document.getElementById("myTable");
var row = x.insertRow(x.rows.length);
var cell = row.insertCell(0);
var a = document.createElement("input");
a.setAttribute("type","text");
cell.appendChild(a);
var cell1 = row.insertCell(1);
var b = document.createElement("input");
b.setAttribute("type","number");
b.setAttribute("id","billAmt");
b.setAttribute("name","number");
b.setAttribute("onkeyup","calc(this)");
cell1.appendChild(b);
var cell2 = row.insertCell(2);
var c = document.createElement("input");
c.setAttribute("type","date");
cell2.appendChild(c);
var cell3 = row.insertCell(3);
var d = document.createElement("input");
d.setAttribute("type","text");
cell3.appendChild(d);
var cell4 = row.insertCell(4);
var e = document.createElement("button");
e.innerText = "Delete";
e.setAttribute("id","delBtn");
e.setAttribute("onclick","removeRow(this)");
cell4.appendChild(e);
}
function removeRow(elem) {
var table = elem.parentNode.parentNode.parentNode;
var rowCount = table.rows.length;
var row = elem.parentNode.parentNode;
row.parentNode.removeChild(row);
}
var x = 0;
var y = 0;
var z = 0;
function calc(obj) {
var e = obj.id.toString();
if (e == 'startAmt') {
x = Number(obj.value);
y = Number(document.getElementById('billAmt').value);
} else {
x = Number(document.getElementById('startAmt').value);
y = Number(obj.value);
}
z = x + y;
document.getElementById('total').value = z;
document.getElementById('totalAmt').innerHTML = z;
}
</script>
</body>
</html>
The result I get: