- Posts: 123
- Thank you received: 9
Ask the community, share ideas, and connect with other LimeSurvey users!
paulfiner wrote: I have a list of 20 brands that respondents can say whether they've heard of or not in a multi-choice question.
I am only interested in 3 of these brands which I have set up an equation to populate a hidden multi-choice question for.
If only 1 of the 3 brands is mentioned I need to use it in a following question.
If 2 or 3 brands are mentioned I need to randomly pick one of them to use later on.
If none of the brands are mentioned I need to pick a random one from all 3.
I'm using v3.25 and have seen the various workarounds in the forum and manual but am struggling to work out the logic.
Thanks,
Paul
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify the questions var qID = '{QID}'; var thisQuestion = $('#question'+qID); var textQuestion = thisQuestion.nextAll('.text-short:eq(0)'); var nextMultiOpt = thisQuestion.nextAll('.multiple-opt:eq(0)'); var nMOID = nextMultiOpt.attr('id').replace(/question/, ''); // Identify the sub-question codes to target var sqCodes = [4, 5, 9]; // Listener on the checkboxes $(':checkbox', thisQuestion).on('change', function(e) { // Reset the hidden questions $(':text', textQuestion).val(''); $(':checkbox', nextMultiOpt).prop('checked', false).trigger('change'); // Build an array of checked codes (use all if none are checked) var checkedCodes = []; $.each(sqCodes, function(i, val) { if($(':checkbox[id$="X'+qID+val+'"]').is(':checked')) { checkedCodes.push(val); } }); var randomCodes = sqCodes; if(checkedCodes.length > 0) { randomCodes = checkedCodes; } // Randomize the codes shuffleArray(randomCodes); // Load the hidden questions var selectedCode = randomCodes[0]; $(':text', textQuestion).val(selectedCode); $(':checkbox[id$="X'+nMOID+selectedCode+'"]', nextMultiOpt).prop('checked', true).trigger('change'); }); }); 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>