8 Replies Latest reply on May 2, 2011 1:36 PM by csb102155

# Check Boxes and Javascripts

I have a form where there is a table, and inside the table is a row for a Check box for “Sub to Sub”, a row for Work performed by Sub-Contractor Description, a row for Original \$\$ Amount, and a row for Final \$\$ Amount Paid.  The Original Amount and Final Amount Paid totals up at the bottom of the page.

The user would like to have it set that if the Sub-Contractor selects a check box under “Sub to Sub” row, the dollar amount that is entered for the Original Amount and Final Amount Paid for that row does not add into the Total amounts at the bottom of the page.

Is it possible to write a Javascript for this and if so, what would it be?

• ###### 1. Re: Check Boxes and Javascripts

Yes, it's possible. Are you currently using JavaScript to preform the total amounts calculation? If so, can you post it?

• ###### 2. Re: Check Boxes and Javascripts

Hi George:  Yes, my Javascripts for the totals of the 2 columns are as follows:

The Original Amount Column

var f = this.getField("OA1");
var a = f.getArray();
var sum = 0;
for (i = 0; i < a.length; i ++)
sum += +a[i].value; if (sum != 0)
event.value = sum;
else
event.value = ""

The Final Amount Paid Column

var f = this.getField("FAP1");
var a = f.getArray();
var sum = 0;
for (i = 0; i < a.length; i ++)
sum += +a[i].value; if (sum != 0)
event.value = sum;
else
event.value = ""

• ###### 3. Re: Check Boxes and Javascripts

Ok, you just have to add a check for the value of the corresponding check box, and if it's "Off", add the value of the text field to the sum. Something like:

```(function () {

var f1 = this.getField("OA1");
var a1 = f1.getArray();

// Get the "Sub to Sub" check boxes
var a2 = getField("cbSubToSub").getArray();

var sum = 0;

for (i = 0; i < a1.length; i++) {

// Only add the value if the check box is not selected
if (a2[i].value === "Off") {
sum += +a1[i].value;
}
}

if (sum != 0) {
event.value = sum;
} else {
event.value = ""
}

})();
```

I used "cbSubToSub" for the prefix for the check boxes, so you'd have to change this to whatever you're using.

• ###### 4. Re: Check Boxes and Javascripts

Hi George:

I tried to use the script you posted, but it does not work.  Where should the script go, in the checkbox field?  Or the Total field?

Thanks much

Connie

• ###### 5. Re: Check Boxes and Javascripts

It is intended to replace the first calculate script, and you could then modify the second one similarly. How have you named the Sub to Sub check boxes?

• ###### 6. Re: Check Boxes and Javascripts

Okay.  I put it in the first calculated script which is the total for Original Amount (OA1).  The checkboxes are named Checkbox1.1, Checkbox1.2, etc. But it's not calculating at all.  Any suggestions?

(function () {

var f1 = this.getField("OA1");
var a1 = f1.getArray();

// Get the "Sub to Sub" check boxes
var a2 = getField("CheckBox1").getArray();

var sum = 0;

for (i = 0; i < a1.length; i++) {

// Only add the value if the check box is not selected
if (a2[i].value === "Off") {
sum += +a1[i].value;
}
}

if (sum != 0) {
event.value = sum;
} else {
event.value = ""
}

})();

Thanks

Connie

• ###### 7. Re: Check Boxes and Javascripts

I'd have to look at the form to see why it's not working. I'd be happy to take a look if you're free to email it to me at: acroscript at gmail dot com

• ###### 8. Re: Check Boxes and Javascripts

Ok, I sent to the email you gave me.  Let me know if you don't receive it and thanks much for taking a look at the form.

Connie