Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How should I LOOP my Script to avoid over-clicking in answering many calculations?

1. ## How should I LOOP my Script to avoid over-clicking in answering many calculations?

Hello,

I am wondering how I would loop a calculation statement on the following JavaScript, so that the user would only have to click once to retrieve all the calculations?

Some calculations require previous results to continue and currently I can only achieve this by clicking the submit form button 4 times to complete the calculations. I'm sure there is a way of completing such but am unsure, as I'm so new to JavaScript.

Any help or further direction would be great, as I feel I've hit a brick wall. Comment back if you need any further elaboration.

Code:
```<script language="JavaScript">

// script to convert number to thousandths, currency and decimal places

String.prototype.reverse = function () {return this.split('').reverse().join('')};
function Dollars (as) {this.ammount = typeof as == 'number' ? as : Number(as.toString().replace(/[\$,]/g, ''))};
Dollars.prototype.valueOf = function () {return this.ammount};
Dollars.prototype.toString = function () {
if (isNaN (this.ammount)) return NaN.toString();
var n = this.ammount.toFixed(2).split ('.');
return [(this.ammount < 0 ? '-' : ''), '\$', (n[0].length > 3 ? n[0].reverse().match(/\d{1,3}/g).join(',').reverse() : n[0]),'.', n[1]].join('');
}

// end thousandths, currency and decimal places script

// begin 'showpay' calculation script
function showpay() {

// validation for data entry
if ((document.calc.a.value == null || document.calc.a.value.length == 0) ||
(document.calc.b.value == null || document.calc.b.length == 0) ||
(document.calc.c.value == null || document.calc.c.value.length == 0) ||
(document.calc.d.value == null || document.calc.d.value.length == 0) ||
(document.calc.e.value == null || document.calc.e.value.length == 0))

}
else

// declare variables, if all data required is entered

{
var aa = document.calc.a.value;
var ab = document.calc.b.value;
var ac = document.calc.c.value;
var ae = document.calc.e.value;

var af = document.calc.f.value;
var ag = document.calc.g.value;
var ah = document.calc.h.value;

var ai = document.calc.i.value;
var aj = document.calc.j.value;
var ak = document.calc.k.value;

var al = document.calc.l.value;
var am = document.calc.m.value;
var an = document.calc.n.value;
var ao = document.calc.o.value;

var ap = document.calc.p.value;
var aq = document.calc.q.value;
var ar = document.calc.r.value;
var as = document.calc.s.value;

var at = document.calc.t.value;
var au = document.calc.u.value;
var av = document.calc.v.value;
var aw = document.calc.w.value;

// calculations

document.calc.f.value = ab * ac;
document.calc.g.value = ((ac * ad).toFixed( 2 ));
document.calc.h.value = ((ae / ag).toFixed( 2 ));

document.calc.i.value = ((ah / 4.3).toFixed( 2 ));
document.calc.j.value = ((ai / 4.3).toFixed( 2 ));
document.calc.k.value = ((ah / 52).toFixed( 2 ));

document.calc.l.value = aa * 1;
document.calc.m.value = new Dollars(aa * af);
document.calc.o.value = new Dollars(aa * ag);

document.calc.p.value = aa * 4.3;
document.calc.q.value = new Dollars(af * ap);
document.calc.s.value = new Dollars(ag * ap);

document.calc.t.value = ap * 12;
document.calc.u.value = new Dollars(af * at);
document.calc.w.value = new Dollars(ag * at);

}
}
// end showpay script
</script>```

• I'm not sure I understand why you need to push submit 4 times, because of validation?
I would use onblur event and add showpay to each input but a problem can occure with that when all fields are empty and user start to fill the values because will show alert message each time onblur event occure.
probably you can solve this by adding a global flag variable or if you add onblur event only to the last field in the form.

best regards

• This guy has completely ignored the advice/help given in previous posts, inc. http://www.codingforums.com/showthread.php?t=169639

You must calculate the value of a form field before you access it.

var af = document.calc.f.value;
var ag = document.calc.g.value;
var ah = document.calc.h.value;

These are all outputs of the calculation, not inputs.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•