- Posts: 1
- Thank you received: 0
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.
- sebinbenjamin
- Topic Author
- Offline
- New Member
Less
More
7 years 6 months ago - 7 years 6 months ago #140907
by sebinbenjamin
Equation Question type - Using JavaScript, to generate the question. was created 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.
I am using limesurvey Version 2.50+ Build 160810 running in a docker container.
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: 7 years 6 months ago by sebinbenjamin. Reason: added info
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3690
7 years 6 months ago - 7 years 6 months ago #140909
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Equation Question type - Using JavaScript, to generate the question.
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: 7 years 6 months ago by tpartner.
The following user(s) said Thank You: sebinbenjamin
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12713
- Thank you received: 3924
7 years 6 months ago #140912
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Equation Question type - Using JavaScript, to generate the question.
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
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.