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!!!
Copy link to clipboard
Copied
What are the names of the radio buttons?
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?
Copy link to clipboard
Copied
So it's "question 1", "question 2", etc.? How many such questions are there?
Copy link to clipboard
Copied
yes it is question 1 - group 1 with 3 options going down to question number 28.
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;
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.
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 "...
Copy link to clipboard
Copied
Sorry, yes it was group ... thank you it works a treat. Thank you for your help with this.