- Posts: 8
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
[Q] Set answer value by Javascript
- w14a
- Topic Author
- Offline
- New Member
Less
More
7 years 4 months ago - 7 years 4 months ago #143948
by w14a
[Q] Set answer value by Javascript was created by w14a
Hello community!
Recently I have joined limesurvey world. My question is probably well known, but I still unable to get the result even reading through many pages of manual and FAQ's.
Scenario: The question (text display) has javascript calculations that take some answers from survey's questions from different pages (groups) across whole survey and finally draw graph based on those calculations. Say, function produces A, B and C values (i.e. fills A,B,C javascipt variables) and displays them as a bar chart. This part is OK, well done. Next in the same script I need those A, B and C values to be saved into three hidden questions somewhere placed in survey. Thus, exporting data to CSV I wish to get all responder's answers and already calculated A, B, C values in respective "answers" fields. This par of the scenario does not work by use $('#answerA').val(A) or similar expression. Where is a trick? How it should be properly constructed? Thanks!
Recently I have joined limesurvey world. My question is probably well known, but I still unable to get the result even reading through many pages of manual and FAQ's.
Scenario: The question (text display) has javascript calculations that take some answers from survey's questions from different pages (groups) across whole survey and finally draw graph based on those calculations. Say, function produces A, B and C values (i.e. fills A,B,C javascipt variables) and displays them as a bar chart. This part is OK, well done. Next in the same script I need those A, B and C values to be saved into three hidden questions somewhere placed in survey. Thus, exporting data to CSV I wish to get all responder's answers and already calculated A, B, C values in respective "answers" fields. This par of the scenario does not work by use $('#answerA').val(A) or similar expression. Where is a trick? How it should be properly constructed? Thanks!
Last edit: 7 years 4 months ago by w14a.
The topic has been locked.
- Deusdeorum
- Offline
- Senior Member
Less
More
- Posts: 72
- Thank you received: 17
7 years 4 months ago #143951
by Deusdeorum
Replied by Deusdeorum on topic [Q] Set answer value by Javascript
Could attach a sample survey? It seems like are right with your javascript.. Anyhow, this is what I would have done in 2.50+ with default template, create a multiple numerical input question and then simply fill those with your variables:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // this is your variables, random here var A = Math.floor((Math.random() * 10) + 1); var B = Math.floor((Math.random() * 10) + 1); var C = Math.floor((Math.random() * 10) + 1); //insert into your multiple numerical question $('#answer753561X33X3261').val(A); $('#answer753561X33X3262').val(B); $('#answer753561X33X3263').val(C); }); </script>
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
7 years 4 months ago - 7 years 4 months ago #143964
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 [Q] Set answer value by Javascript
To expand on Deusdeorum's post a bit - if placing the script in the source of the multiple numerical, you can use the {QID} placeholder to avoid hard-coding the IDs.
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // this is your variables, random here var A = Math.floor((Math.random() * 10) + 1); var B = Math.floor((Math.random() * 10) + 1); var C = Math.floor((Math.random() * 10) + 1); //insert into your multiple numerical question $('#question{QID} input[type="text"]:eq(0)').val(A); $('#question{QID} input[type="text"]:eq(1)').val(B); $('#question{QID} input[type="text"]:eq(2)').val(C); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 4 months ago by tpartner.
The topic has been locked.
- w14a
- Topic Author
- Offline
- New Member
Less
More
- Posts: 8
- Thank you received: 0
7 years 4 months ago #144018
by w14a
Replied by w14a on topic [Q] Set answer value by Javascript
Hi,
Thanks for proposals.
The typical tpartner example works for me only if the script is placed within that multiple numeric question body. Script sets answers' values, no problem.
But if the same script (or part of it) sits within another question's body (in my case - text_display type question), then the values are not set (transfered) to the other question answers' field. These stay empty...
Thanks for proposals.
The typical tpartner example works for me only if the script is placed within that multiple numeric question body. Script sets answers' values, no problem.
But if the same script (or part of it) sits within another question's body (in my case - text_display type question), then the values are not set (transfered) to the other question answers' field. These stay empty...
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
7 years 4 months ago #144022
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 [Q] Set answer value by Javascript
Yes, that is correct because {QID} returns the ID of the question that the placeholder resides in.
I'm not sure why you would but, if you want to place the script in another question, you will need to hard-code the ID(s) as in Deusdeorum's example or something like this (where 1234 is the question ID ):
I'm not sure why you would but, if you want to place the script in another question, you will need to hard-code the ID(s) as in Deusdeorum's example or something like this (where 1234 is the question ID ):
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // this is your variables, random here var A = Math.floor((Math.random() * 10) + 1); var B = Math.floor((Math.random() * 10) + 1); var C = Math.floor((Math.random() * 10) + 1); //insert into your multiple numerical question $('#question1234 input[type="text"]:eq(0)').val(A); $('#question1234 input[type="text"]:eq(1)').val(B); $('#question1234 input[type="text"]:eq(2)').val(C); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.