- Posts: 123
- Thank you received: 9
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify the questions var thisQuestion = $('#question{QID}'); var randomProducts = $(thisQuestion).nextAll('.multiple-short-txt:eq(0)'); // Listener on the array $('input:radio', thisQuestion).on('click', function(e) { // Build an array of products var products = []; $('input:radio:checked', thisQuestion).each(function(i) { if($(this).val() < 3) { products.push($.trim($(this).closest('tr').find('.answertext').text())); } }); // Randomize the array shuffleArray(products); // Load the hidden question inputs $('input:text:eq(0)', randomProducts).val(products[0]).trigger('keyup'); $('input:text:eq(1)', randomProducts).val(products[1]).trigger('keyup'); }); }); 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).on('ready pjax:scriptcomplete',function(){ // Identify the questions var thisQuestion = $('#question{QID}'); var randomProducts = $(thisQuestion).nextAll('.multiple-short-txt:eq(0)'); // Listener on the array $('input:radio', thisQuestion).on('click', function(e) { // Build an array of products var products = []; $('input:radio:checked', thisQuestion).each(function(i) { if($(this).val() < 3) { products.push(($(this).closest('tr').index()+1)); } }); // Randomize the array shuffleArray(products); // Load the hidden question inputs $('input:text:eq(0)', randomProducts).val(products[0]).trigger('keyup'); $('input:text:eq(1)', randomProducts).val(products[1]).trigger('keyup'); }); }); 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>