Click to See Complete Forum and Search --> : Help!!!


The
04-30-2003, 03:34 PM
I need a javascript that will calculate a total when data is entered into text fields. I have tried two or three different ones but none of them work.

pyro
04-30-2003, 03:35 PM
Post what you tried, and we can point out what needs to be changed... :)

havik
04-30-2003, 03:39 PM
Do a search in this forum for it, it's come up before.
try this as well:
http://javascript.internet.com/calculators/

havik

AdamBrill
04-30-2003, 03:42 PM
Or you could try this:<html>
<head>
<script language=javascript>
function addThemUp(){
total=0;
total+=parseInt(document.form1.one.value);
total+=parseInt(document.form1.two.value);
total+=parseInt(document.form1.three.value);
document.form1.answer.value=total;
}
</script>
</head>
<body>
<form name=form1>
<input name=one type=text>+<input name=two type=text>+<input name=three type=text>=<input name=answer type=text><input type=button onclick="addThemUp()" value=add>
</form>
</body>
</html>

The
04-30-2003, 03:50 PM
Here's One

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_total() {
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.TOTAL.value ) {
if( t == 0 ) {
pmt.TOTAL.value=""
} else {
tp = t * 100;
tp1 = Math.floor( t ) * 100;
if(( tp - tp1 ) == 0 ) {
pmt.TOTAL.value = t + ".00";
} else {
tp1 = Math.floor( t * 10 ) * 10;
if(( tp - tp1 ) == 0 ) {
pmt.TOTAL.value = t + "0";
} else {
pmt.TOTAL.value = t;
}
}
}
}
setTimeout( "calculate_total()", 100 );
}
</script>

Here's another

function sumFields(){ document.sumForm.product_subtotal.value = (document.sumForm.product1.value-0)+ (document.sumForm.product2.value-0)+ (document.sumForm.product3.value-0)+ (document.sumForm.product4.value-0)+ (document.sumForm.product5.value-0)+ (document.sumForm.product6.value-0)+ (document.sumForm.product7.value-0)+ (document.sumForm.product8.value-0)+ (document.sumForm.product9.value-0)+ (document.sumForm.product10.value-0); }

(On the first one I changed the amount names when I tried to implement it. The version you see is the original, without the changes I made. The second one contains the names of the text boxes users will put the prices in.)

Shampie
04-30-2003, 03:51 PM
this is not a solution?

function dothis(){

Math.round(form.anwser.value) =
Math.round(form.a.value) + Math.round(form.b.value) + Math.round(form.c.value)
}


on a link or button or somewhere..
onClick="dothis()" ??

AdamBrill
04-30-2003, 05:55 PM
Did you try my method? I think that will do what you are looking for... ;)