Hey guys.
What i've created is an automatic calculator using JS. I have three fields that a user enters numbers into, and then in the "subtotal" column It automatically adds up the three fields and displays the result.
The problem I have is, I want another field called "total", that will take the "subtotal" amount field (which has added up the 3 fields) and display the subtotal amount field into the "total" field.
Here is my code below, im lost so please help if you can.
<script language="JavaScript">
function cal( amt, t ) {
var len = amt.length;
if( len > 0 && amt.substring( 0, 1 ) == "$" ) {
amt = amt.substring( 1, len );
}
if( parseFloat( amt ) > 0 ) {
if( parseFloat( t ) > 0 ) {
t = parseFloat( t ) + parseFloat( amt );
} else {
t = parseFloat( amt );
}
}
return t;
}
function strip_amt( amt ) {
var amt_char = "";
var new_amt = "";
for( var c=1; c<=amt.length; c++ ) {
amt_char = amt.substring( c-1, c );
if( amt_char == "," ) {
//do nothing
} else {
new_amt = new_amt+amt_char;
}
}
amt = new_amt;
return amt;
}
function calculate_subtotal() {
var t = 0;
var pmt = document.PMTS;
var num = parseInt( pmt.NUM_PAY.value );
var tp
var tp1
for( var i=1; i<=num; i++ ) {
if( i == 1 ) {
if( pmt.NUM1.value == "I" ) {
amt = pmt.AMT1.value;
amt = strip_amt( amt );
t = cal( amt, t );
}
} else if( i == 2 ) {
if( pmt.NUM2.value == "I" ) {
amt = pmt.AMT2.value;
amt = strip_amt( amt );
t = cal( amt, t );
}
} else if( i == 3 ) {
if( pmt.NUM3.value == "I" ) {
amt = pmt.AMT3.value;
amt = strip_amt( amt );
t = cal( amt, t );
}
}
}
t = t * 100;
t = Math.round( t );
t = t / 100;
if( t != pmt.SUBTOTAL.value ) {
if( t == 0 ) {
pmt.SUBTOTAL.value=""
} else {
tp = t * 100;
tp1 = Math.floor( t ) * 100;
if(( tp - tp1 ) == 0 ) {
pmt.SUBTOTAL.value = t + ".00";
} else {
tp1 = Math.floor( t * 10 ) * 10;
if(( tp - tp1 ) == 0 ) {
pmt.SUBTOTAL.value = t + "0";
} else {
pmt.SUBTOTAL.value = t;
}
}
}
}
setTimeout( "calculate_subtotal()", 100 );
}
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body onload=document.PMTS.AMT1.focus()>
<form name=PMTS id=PMTS method=POST>
<input id=NUM_PAY name=NUM_PAY type=hidden value="3">
<input id=NUM1 name=NUM1 type=hidden value="I">
<input id=NUM2 name=NUM2 type=hidden value="I">
<input id=NUM3 name=NUM3 type=hidden value="I">
<table cellpadding=4 cellspacing=0 border=0>
<tr>
<td width="80">
<span class=BodyText>Amount [1]</font><br>
<input id=AMT1 name=AMT1 size=12 onChange="calculate_subtotal()">
</td>
<td width="80">
<span class=BodyText>Amount [2]</font><br>
<input id=AMT2 name=AMT2 size=12 onChange="calculate_subtotal()">
</td>
<td width="80">
<span class=BodyText>Amount [3]</font><br>
<input id=AMT3 name=AMT3 size=12 onChange="calculate_subtotal()" >
</td>
<td width="72">
<span class=BodyText><b>Total</b></font><br>
<input id=TOTAL name=TOTAL size=12 onChange="calculate_subtotal()" readonly>
</td>
<td width="78">SUBTOTAL<br>
<input id=SUBTOTAL name=SUBTOTAL size=12 readonly></td>
</table>
</form>
</body>
</html>