JavaScript question - how do you calculate specific fields

New Here ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

I am new to Java Script and am trying to work out the following

I have a form with radio buttons.  And a variety of skills observations. Each observation has been marked as a group with different values 0, 1 or 2 depending on the level.

I would like my total box to calculate the number of times the '2' button has been selected and '1' button etc. Help!!!

TOPICS
Acrobat SDK and JavaScript, Windows

Views

107

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
Most Valuable Participant ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

What are the names of the radio buttons?

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
New Here ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

I have named each group after the question number for example question 1 = group 1 and each have 3 values '2' , '1' and '0' does that help?

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
Most Valuable Participant ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

So it's "question 1", "question 2", etc.? How many such questions are there?

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
New Here ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

yes it is question 1 - group 1 with 3 options going down to question number 28.

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
Most Valuable Participant ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

OK, so to count the number of times "2" is selected, for example, you can use this code as the custom calculation script of a text field that will display the result:

var total = 0;

for (var i=1; i<=28; i++) {

    var f = this.getField("question "+i);

    if (f.valueAsString=="2") total++;

}

event.value = total;

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
New Here ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

thank you , can you tell me what the 'i' and 'f' is and presumably on this getField ("question "+i) I can change question to the name of the field? Sorry trying to get my head around javascript as have a design background not programming.

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
Most Valuable Participant ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

The "i" variable is a counter. The "f" variable points to a Field object.

If the fields are really named "question 1", "question 2", etc., as you said, then you should not change it.

Your replies on this were not very clear, I have to say... If they are named "group 1", "group 2", etc., then change it to "group "...

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
New Here ,
Mar 19, 2018 Mar 19, 2018

Copy link to clipboard

Copied

LATEST

Sorry, yes it was group ... thank you it works a treat. Thank you for your help with this.

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