2 Replies Latest reply on Feb 13, 2014 3:26 PM by LukeyB

# Calculate value based upon checkbox selection

G'day people,

I've got myself a bit stuck on a job I've got here, this javascript is getting the better of me.

The idea is that this script takes the sub total, and adds a creditcard surcharge (as a percentage) based upon a checkbox selection, then spits out the value to be entered into another field.

If anyone could have a quick look over the following and let me know where I've gone wrong, it would be appreciated.

---

var subtotal = this.getField("SubTotal").value;

var mastercardTick = this.getField("MasterCard");

var visaTick = this.getField("Visa");

var amexTick = this.getField("Amex");

var surcharge = this.getField("CreditSurcharge").value;

var creditcard = this.getField("PayCredit").value;

if (creditcard.value === "Off") {

event.value = 0;

} else {

if (mastercardTick.value === "Yes") {

surcharge.value= (1.2 / 100) * subtotal.value;

} else if (visaTick.value === "Yes") {

surcharge.value = (1.2 / 100) * subtotal.value;

} else if (amexTick.value === "Yes") {

surcharge.value = (3.75 / 100) * subtotal.value;

} else {

event.value = 0;

}

}

---

• ###### 1. Re: Calculate value based upon checkbox selection

For lines like these:

surcharge.value= (1.2 / 100) * subtotal.value;

shouldn't they be like:

event.value= (1.2 / 100) * subtotal.value;

The variable surcharge is set at the beginning of the script to the value of the field, so setting surcharge.value doesn't make any sense.

• ###### 2. Re: Calculate value based upon checkbox selection

Thanks George, I've given this a whirl and got very close... I got a NaN error, and found I was calling "SubTotal" a value twice, which was playing funny buggers!

I've got rid of one part (the if creditcard.value === "Off") as it wasn't acting like it should. I think I had to put it in a seperate function, to be called seperately then the rest of the code... However, I need to get the job out and it wasn't crucial.

Thanks for your help mate, appreciated.

For anyone else that's interested, amended code is as follows:

---

var subtotal = this.getField("SubTotal");

var mastercardTick = this.getField("MasterCard");

var visaTick = this.getField("Visa");

var amexTick = this.getField("Amex");

// var surcharge = this.getField("CreditSurcharge").value;

var creditcard = this.getField("PayCredit").value;

if (mastercardTick.value === "Yes") {

event.value = (1.2 / 100) * subtotal.value;

} else if (visaTick.value === "Yes") {

event.value = (1.2 / 100) * subtotal.value;

} else if (amexTick.value === "Yes") {

event.value = (3.75 / 100) * subtotal.value;

} else {

event.value = 0;

}

---

Not the nicest, but hey, it worked.