I am fairly new to Javascript, and working with the DOM. I'm trying to add the user's input values to a total. Every time they add a value for credit or debit, it will add it to the total. Every attempt I've made, results in NaN, undefined or some other error. Do I need a for loop or is there a simpler way to do this?
Here is the full code: JSFiddle
My Javascript snippet (to add totals):
if (document.getElementsByTagName('select')[0].selectedIndex == 1) {
transactions.appendChild(debit);
text = document.createTextNode("debit");
td2.appendChild(text);
var money = document.querySelector('input[type="number"]').value;
money = parseFloat(money).toFixed(2);
var total = 0;
for (var i = 0; i < money.length; i++) {
if (parseInt(money[i].value))
total += parseInt(money[i].value);
}
document.querySelector('.right > .total.debits').innerHTML = total;
evt.target.reset();
}
My HTML:
<body>
<section class="wrapper">
<h1>Transaction Tracker</h1>
<form class="transaction-frm">
<fieldset>
<legend>Add Transaction</legend>
<div class="frm-group">
<input class="frm-control" type="text" name="description" size="30" placeholder="description" />
</div>
<div class="frm-group">
<select class="frm-control" name="type">
<option value="">type</option>
<option value="debit">debit</option>
<option value="credit">credit</option>
</select>
</div>
<div class="frm-group">
<i class="edit fa fa-dollar"></i>
<input class="frm-control" type="number" name="currency" min="0" max="9999" step="0.01" size="4" placeholder="0.00" />
</div>
<div class="frm-group">
<input class="frm-control" type="submit" value="add" />
</div>
<div class="error"></div>
</fieldset>
</form>
<h2>Transactions</h2>
<table class="transactions">
<thead>
<tr>
<td colspan="4" class="right">
Total debits: <span class="total debits">$0.00</span>
Total credits: <span class="total credits">$0.00</span>
</td>
</tr>
<tr>
<th>Description</th>
<th>Type</th>
<th class="amount">Amount</th>
<th class="tools">Tools</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</section>
<script src="js/main.js"></script>