Skip to main content
Participant
March 18, 2020
Question

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

  • March 18, 2020
  • 1 reply
  • 863 views

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.

This topic has been closed for replies.

1 reply

Participant
March 18, 2020

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.

Thom Parker
Community Expert
Community Expert
March 24, 2020

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 PDFScriptingUse the Acrobat JavaScript Reference early and often