Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Equation Question type - Using JavaScript, to generate the question.

More
10 years 1 month ago - 10 years 1 month ago #140907 by sebinbenjamin
Hi,

I need to calculate a score based on survey answers and also store it to the database. I have understood that the equation question type does this for you. I am able to change the question text by using javascript document.write(). But it seems that the result is not being stored in the survey response. Please help regarding the same. I was not able to find or understand any solution to the same in the forum and documentations.

This is my equation question.
Code:
<script> var f01,fO2,f03,sum; //Yes/No Question 1 if('{F01.NAOK}'=='Y') f01 = 1; else if('{F01.NAOK}'=='N') f01 = -1; else f01 = 0; //Yes/No Question 2 if('{F02.NAOK}'=='Y') f02 = 1; else if('{F02.NAOK}'=='N') f02 = -1; else f02 = 0; //Yes/No Question 3 if('{F03.NAOK}'=='Y') f03 = 1; else if('{F03.NAOK}'=='N') f03 = -1; else f03 = 0; sum = f01 + f02 + f03; document.write(sum); </script>

I am using limesurvey Version 2.50+ Build 160810 running in a docker container.
Last edit: 10 years 1 month ago by sebinbenjamin. Reason: added info
The topic has been locked.
More
10 years 1 month ago - 10 years 1 month ago #140909 by tpartner
You cannot use JavaScript to insert values into equation type questions. If you need to use JS, insert the values into a hidden short-text question.

Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Hide this question $('#question{QID}').hide(); var f01,fO2,f03,sum; //Yes/No Question 1 if('{F01.NAOK}'=='Y') f01 = 1; else if('{F01.NAOK}'=='N') f01 = -1; else f01 = 0; //Yes/No Question 2 if('{F02.NAOK}'=='Y') f02 = 1; else if('{F02.NAOK}'=='N') f02 = -1; else f02 = 0; //Yes/No Question 3 if('{F03.NAOK}'=='Y') f03 = 1; else if('{F03.NAOK}'=='N') f03 = -1; else f03 = 0; sum = f01 + f02 + f03; $('#question{QID} input[type="text"]').val(sum); }); </script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 1 month ago by tpartner.
The following user(s) said Thank You: sebinbenjamin
The topic has been locked.
More
10 years 1 month ago #140912 by Joffm
Hi, sebinbenjamin,

and here the same if you want to use an expression question:
{sum(if(F01.NAOK=='Y',1,if(F01.NAOK=='N',-1,0)),if(F02.NAOK=='Y',1,if(F02.NAOK=='N',-1,0)),if(F03.NAOK=='Y',1,if(F03.NAOK=='N',-1,0)))}

Regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: sebinbenjamin
The topic has been locked.
Moderators: tpartner, holch

Lime-years ahead

Online-surveys for every purse and purpose