Calculate Sum of Input Fields
JS to calculate the sum of multiple form text input fields, and output the total into another input field.
by akmiecik
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div>
<h2>Pricing</h2>
<p><strong>Cost of Print Portion</strong><br />
$ <input onblur="findTotal()" type="text" class="amount" name="printcost" /></p>
<p><strong>Cost of Digital Portion</strong><br />
$ <input onblur="findTotal()" type="text" class="amount" name="digitalcost" /></p>
<p><strong>Cost of DMS Portion</strong><br />
$ <input onblur="findTotal()" type="text" class="amount" name="dmscost" /></p>
<p><strong>Cost of Pre-Print Portion</strong><br />
$ <input onblur="findTotal()" type="text" class="amount" name="preprintcost" /></p>
<p><strong>Overall Cost of Order <span class="required">*</span></strong><br />
$ <input type="text" name="totalordercost" id="totalordercost" /></p>
</div>
CSS
body { font-family:Helvetica, Arial, sans-serif; font-size:11px; }
JavaScript
function findTotal(){
var arr = document.getElementsByClassName('laps');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseFloat(arr[i].value))
tot += parseFloat(arr[i].value);
}
document.getElementById('totalordercost').value = tot;
}