• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need Javascript to count number of questions answered to get a percentage total

New Here ,
Mar 18, 2020 Mar 18, 2020

Copy link to clipboard

Copied

I have a PDF that I am creating in Adobe Acrobat that is a questionaire. I used radio buttons so that the user can easily select the score of the answer to each quesion between the range of 1-10 (each answer has a hidden button preselected giving it a score of 0).

 

There are 6 Sections total.

Each Section has 4 questions.

So, a total of 24 questions

 

I can't figure out the javascript to count how many questions have been answered in each section (basically a score between 1-10). The goal is to give a balanced score at the end of the pdf that takes into account only the questions answered.

 

I hope this makes sense. I have no coding experience and have been totally lost browsing the internet for answers.

TOPICS
Acrobat SDK and JavaScript

Views

569

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 18, 2020 Mar 18, 2020

Copy link to clipboard

Copied

If there is anyone willing to take a crack at this the name of the first section fields are:

"DRTQOne"

"DRTQTwo"

"DRTQThree"

"DRTQFour"

 

I figure if I can get one section working I can get the rest to work.

Votes

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
Community Expert ,
Mar 23, 2020 Mar 23, 2020

Copy link to clipboard

Copied

LATEST

So an answered question has a value > 0, Correct?

So the script needs to count the number of values > 0;

 

The general technique is to loop over the question fields, counting the ones that meet the condition. 

One way to do this is to list all the fields in an array

 

var aFldNames = ["A", "B", "C"];

var nCnt = 0;

for(var i=0;i<aFldNames.length;i++)

{

    oFld = this.getField(aFldNames[i]);

    if(oFld.value > 0) nCnt++;

}

 

I prefer to name the fiels so that getting them is automatic. 

"Question.Sec1.Q1", "Question.Sec1Q2", "Question.Sec2Q1", etc.

Then use this variation on the script:

 

var aFlds = this.getField("Question");

var nCnt = 0;

aFlds.forEach(function(a){if(a.value>0)nCnt++;});

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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