- Posts: 11
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
$(document).ready(function() { /* define variable for this question */ var thisQuestion = $('#question{QID}'); /* Hide it */ thisQuestion.hide(); /* This gets the text input that was put into the first hidden text field by Tony's script */ var text="{INSERTANS:999999X253X31041}"; /* Change this to match your first hidden text question SGQ */ /* NOW UPDATE THE LIST QUESTION */ var updateName='#answer999999X254X3105'; /* Change this to match your first list question SGQ */ var updateCode=''; if(text == 'apples') { updateCode='A'; } else if (text == 'bananas') { updateCode='B'; } else if (text == 'strawberries') { updateCode='C'; } else if (text == 'grapefruit') { updateCode='D'; } $(updateName+updateCode).attr("checked","checked"); /* First we "check" the radio button */ $(updateName+updateCode).click(); /* Then we fake the mouse click, to fire off any conditions in LimeSurvey */ $(updateName+updateCode).change(); /* Then we fake the change, to also make sure any conditions are applied */ }); </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify the questions var thisQuestion = $('#question{QID}'); var thisQuestionID = {QID}; var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)'); var hiddenInput1 = $('input.text:eq(0)', qHidden); var hiddenInput2 = $('input.text:eq(1)', qHidden); var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)'); var qHidden2ID = qHidden2.attr('id').replace(/question/, ''); // Hide the control questions qHidden.hide(); qHidden2.hide(); // Listener on the radios $('input.radio', thisQuestion).on('click', function(e) { handleChecked(); }); function handleChecked() { // Build an array of checked sub-question codes var checkedAnswers = []; $('input.radio:checked', thisQuestion).each(function(i) { if($(this).closest('td.answer-item').is(':last-child')) { var thisSQCode = $(this).closest('tr.answers-list').attr('id').split('X'+thisQuestionID)[1]; checkedAnswers.push(thisSQCode); } }); // Shuffle the array shuffleArray(checkedAnswers); // Reset the hidden multi-choice $('input.checkbox', qHidden2).each(function(i) { $(this).prop('checked', false); $(this).nextAll('input:hidden:eq(0)').val(''); }); // Load the hidden questions with 2 random items from the array $(hiddenInput1).val($.trim($('tr.answers-list[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .answertext').text())); $('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click'); if(checkedAnswers.length > 1) { $(hiddenInput2).val($.trim($('tr.answers-list[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .answertext').text())); $('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click'); } else { $(hiddenInput2).val(''); } // Fire Expression Manager checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type); checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type); $('input.checkbox', qHidden2).each(function(i) { checkconditions(this.value, this.name, this.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 thisQuestionID = {QID}; var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)'); var hiddenInput1 = $('input.text:eq(0)', qHidden); var hiddenInput2 = $('input.text:eq(1)', qHidden); var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)'); var qHidden2ID = qHidden2.attr('id').replace(/question/, ''); // Hide the control questions qHidden.hide(); qHidden2.hide(); // Listener on the checkboxes $('input.checkbox', thisQuestion).on('change', function(e) { handleChecked(); }); function handleChecked() { // Build an array of checked sub-question codes var checkedAnswers = []; $('input.checkbox:checked', thisQuestion).each(function(i) { var thisSQCode = $(this).attr('id').split('X'+thisQuestionID)[1]; checkedAnswers.push(thisSQCode); }); // Shuffle the array shuffleArray(checkedAnswers); // Reset the hidden multi-choice $('input.checkbox', qHidden2).each(function(i) { $(this).prop('checked', false); $(this).nextAll('input:hidden:eq(0)').val(''); }); // Load the hidden questions with 2 random items from the visible multi-choice $(hiddenInput1).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .label-text').text())); $('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click'); if(checkedAnswers.length > 1) { $(hiddenInput2).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .label-text').text())); $('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click'); } else { $(hiddenInput2).val(''); } // Fire Expression Manager checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type); checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type); $('input.checkbox', qHidden2).each(function(i) { checkconditions(this.value, this.name, this.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 thisQuestionID = {QID}; var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)'); var hiddenInput1 = $('input:text:eq(0)', qHidden); var hiddenInput2 = $('input:text:eq(1)', qHidden); var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)'); var qHidden2ID = qHidden2.attr('id').replace(/question/, ''); // Hide the control questions qHidden.hide(); qHidden2.hide(); // Listener on the radios $('input:checkbox', thisQuestion).on('change', function(e) { handleChecked(); }); function handleChecked() { // Build an array of checked sub-question codes var checkedAnswers = []; $('input:checkbox:checked', thisQuestion).each(function(i) { var thisSQCode = $(this).attr('id').split('X'+thisQuestionID)[1]; checkedAnswers.push(thisSQCode); }); // Shuffle the array shuffleArray(checkedAnswers); // Reset the hidden multi-choice $('input:checkbox', qHidden2).each(function(i) { $(this).prop('checked', false); $(this).nextAll('input:hidden:eq(0)').val(''); }); // Load the hidden questions with 2 random items from the array $(hiddenInput1).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .checkbox-label').text())); $('input:checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click'); if(checkedAnswers.length > 1) { $(hiddenInput2).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .checkbox-label').text())); $('input:checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click'); } else { $(hiddenInput2).val(''); } // Fire Expression Manager checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type); checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type); $('input:checkbox', qHidden2).each(function(i) { checkconditions(this.value, this.name, this.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>