I have a 'multiple short text' question where people have to write their answers (there are N short texts to write). Once this is done, I want to select randomly one of those texts to ask questions to that specific text/answer. I am wondering if there is a way to do this?
I have found how to do something similar when the question one takes the answer from is a multiple choice question:
This script will load a hidden short-text question with a random value from the preceding multiple-short-text question. The loading is done when the page is submitted.
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Identify the questions
var thisQuestion = $('#question{QID}');
var qHidden = thisQuestion.nextAll('.text-short:eq(0)');
var hiddenInput = $('input.text', qHidden);
// Hide qHidden
qHidden.hide();
// Listener on submit function
$('#limesurvey').on('submit', function(e) {
// Build an array of input text strings
var q1Answers = [];
$('input[type="text"]', thisQuestion).each(function(i) {
if($.trim($(this).val()) != '') {
q1Answers.push($.trim($(this).val()));
}
});
// Load the hidden question with a random item from the array
var answersLength = q1Answers.length;
$(hiddenInput).val(q1Answers[Math.floor(Math.random()*answersLength)]).trigger('change');
});
});
</script>