I have this script that works for now, where I want to add the loan payments together and the total loan values as well but keep the two types separate.
I feel that I'm not using jQuery to its full potential because I'm copying and pasting the same thing twice and repeating my self with code that is similar.
Am I missing something here? Is there a better way to get selected values from multiple input fields?
UPDATE
I made a mistake and left the id's the same as the <input /> values. So I updated the code to resemble that.
$(document).ready(function() {
getTotalAmount();
});
function getTotalAmount() {
var monthTotal = 0.00;
var total = 0.00;
$('input.monthAmt').each(function() {
monthTotal += parseFloat($(this).val());
console.log("These is something here: " + monthTotal + " ");
});
$('input.loanAmt').each(function() {
total += parseFloat($(this).val());
console.log("These is something here: " + total + " ");
});
$('#monthAmt').text(monthTotal.toFixed(2).toString());
$('#loanAmt').text(total.toFixed(2).toString());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Loan 1:
<label>Monthly Amount: </label>
<input class="monthAmt" value="1" />
<label>Loan Amount: </label>
<input class="loanAmt" value="2" />
</br>
</br>
Loan 2:
<label>Loan Amount: </label>
<input class="monthAmt" value="3" />
<label>Loan Amount: </label>
<input class="loanAmt" value="4" />
</br>
</br>
<div id="totalMonthlyAmt">Total Monthly payments: $<span id="moAmt"></span></div>
<div id="totalLoanAmt">Total Loan Amount: $<span id="lnAmt"></span></div>