- Posts: 11
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
var checkedLength = checkedAnswers.length; hiddenInput1 = checkedAnswers[Math.floor(Math.random()*checkedLength)]; if (checkedLength > 1) { do { hiddenInput2 = checkedAnswers[Math.floor(Math.random()*checkedLength)]; } while(hiddenInput1 == hiddenInput2); } $(hiddenInput).val(hiddenInput1+" "+hiddenInput2);
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the questions var thisQuestion = $('#question{QID}'); var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)'); var hiddenInput1 = $('input.text:eq(0)', qHidden); var hiddenInput2 = $('input.text:eq(1)', qHidden); // Hide qHidden qHidden.hide(); // Class for "Other $('input.text', thisQuestion).closest('.answer-item').addClass('other-item'); // Listener on the checkboxes $('input.checkbox', thisQuestion).on('change', function(e) { handleChecked(); }); // Listener on the "Other" input $('input.text', thisQuestion).on('keyup change', function(e) { setTimeout(function() { handleChecked(); }, 250); }); function handleChecked() { // Build an array of checked answers var checkedAnswers = []; $('input.checkbox:checked', thisQuestion).each(function(i) { if($(this).closest('.answer-item').hasClass('other-item')) { checkedAnswers.push($(this).closest('.answer-item').find('input.text').val()); } else { checkedAnswers.push($.trim($(this).nextAll('.label-text:eq(0)').text())); } }); // Shuffle the array shuffleArray(checkedAnswers); // Load the hidden question with a random 2 items from the array $(hiddenInput1).val(checkedAnswers[0]); if(checkedAnswers.length > 1) { $(hiddenInput2).val(checkedAnswers[1]); } else { $(hiddenInput2).val(''); } // Fire Expression Manager checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type); checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type); } }); function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the questions var thisQuestion = $('#question{QID}'); var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)'); var hiddenInput1 = $('input.text:eq(0)', qHidden); var hiddenInput2 = $('input.text:eq(1)', qHidden); // Hide qHidden qHidden.hide(); // Listener on the radios $('input.radio', thisQuestion).on('click', function(e) { handleChecked(); }); function handleChecked() { // Build an array of checked answers var checkedAnswers = []; $('input.radio:checked', thisQuestion).each(function(i) { if($(this).closest('td.answer-item').is(':last-child')) { checkedAnswers.push($(this).closest('tr.answers-list').find('.answertext').text()); } }); // Shuffle the array shuffleArray(checkedAnswers); // Load the hidden question with 2 random items from the array $(hiddenInput1).val(checkedAnswers[0]); if(checkedAnswers.length > 1) { $(hiddenInput2).val(checkedAnswers[1]); } else { $(hiddenInput2).val(''); } // Fire Expression Manager checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type); checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type); } }); function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } </script>
tpartner wrote: This will load the hidden multi-text question with the array row labels where the last column is checked.
Code:<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the questions var thisQuestion = $('#question{QID}'); var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)'); var hiddenInput1 = $('input.text:eq(0)', qHidden); var hiddenInput2 = $('input.text:eq(1)', qHidden); // Hide qHidden qHidden.hide(); // Listener on the radios $('input.radio', thisQuestion).on('click', function(e) { handleChecked(); }); function handleChecked() { // Build an array of checked answers var checkedAnswers = []; $('input.radio:checked', thisQuestion).each(function(i) { if($(this).closest('td.answer-item').is(':last-child')) { checkedAnswers.push($(this).closest('tr.answers-list').find('.answertext').text()); } }); // Shuffle the array shuffleArray(checkedAnswers); // Load the hidden question with 2 random items from the array $(hiddenInput1).val(checkedAnswers[0]); if(checkedAnswers.length > 1) { $(hiddenInput2).val(checkedAnswers[1]); } else { $(hiddenInput2).val(''); } // Fire Expression Manager checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type); checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type); } }); function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } </script>
Sample survey attached:
If there is only one eligible option, it will be written to the first sub-question of the hidden question.Sometimes when I test the survey and I only claim to know one of the items, the variable qHidden is empty and the item is written in qHidden2 or vice versa.
Alright, thank youtpartner wrote:
If there is only one eligible option, it will be written to the first sub-question of the hidden question.Sometimes when I test the survey and I only claim to know one of the items, the variable qHidden is empty and the item is written in qHidden2 or vice versa.