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!
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>
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.
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...
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
):
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.