help with javascript

Explorer ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

I'm trying to adapt code to my needs but am having problems with it.

I have checkbox fields with numbers as export values and trying to sum those values if checkboxes are checked, here is what I have:

var total = "";
for (var i=1; i<=5; i++) {
if (this.getField("C"+i).valueAsString!= "Off") total = this.getField("C"+i).value;
}
event.value = total;

TOPICS
JavaScript

Views

94

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Feb 04, 2021 Feb 04, 2021
Change the first line to: var total = 0;

Likes

Translate

Translate
Adobe Community Professional ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

You don't need a script for this. At calculation use the option "Value is the sum of the following fields".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

I can't do that because I have other calculations in the same field.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

You're not adding up the values, but overwriting them.

Change this:

total = this.getField("C"+i).value;

To:

total += Number(this.getField("C"+i).valueAsString);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

I tried that already but I'm getting value as string e.g. 100200300 instead of 600.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

Change the first line to:

var total = 0;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

LATEST

Thank you, that worked.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines