Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Using/saving a random element from multiple choice

More
7 years 11 months ago #138124 by Juanoche

tpartner wrote: This script should work for LS version 2.5:

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 the checkboxes
    $('input.checkbox', thisQuestion).on('change', function(e) {
      // Build an array of checked answers
      var checkedAnswers = [];
      $('input.checkbox:checked', thisQuestion).each(function(i) {
        checkedAnswers.push($(this).nextAll('.label-text:eq(0)').text());
      });
 
      // Load the hidden question with a random item from the array
      var checkedLength = checkedAnswers.length;
      $(hiddenInput).val(checkedAnswers[Math.floor(Math.random()*checkedLength)]);
 
      // Fire Expression Manager
      checkconditions(hiddenInput.value, hiddenInput.name, hiddenInput.type);
    });
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...1461.lss
File Size:18 KB


I use a modified script of this one to keep the 3 answers of a multiple choice and work fine but it doesn't work with the Other option activate. I think "Listener on the checkboxes" doesn't work with "Other option", only when check/uncheck subquestions. I need to keep the text in the textbox when Other option is checked. Any suggestion??

This is the script I'm using:

<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 qHidden2 = thisQuestion.nextAll('.text-short:eq(1)');
var qHidden3 = thisQuestion.nextAll('.text-short:eq(2)');
var hiddenInput = $('input.text', qHidden);
var hiddenInput2 = $('input.text', qHidden2);
var hiddenInput3 = $('input.text', qHidden3);

// Hide qHidden
qHidden.hide();
qHidden2.hide();
qHidden3.hide();

// Listener on the checkboxes
$('input.checkbox', thisQuestion).on('change', function(e) {
// Build an array of checked answers
var checkedAnswers = [];
$('input.checkbox:checked', thisQuestion).each(function(i) {
checkedAnswers.push($(this).nextAll('.label-text:eq(0)').text());
});

// Load the hidden question with items from the array
$(hiddenInput).val(checkedAnswers[0]);
$(hiddenInput2).val(checkedAnswers[1]);
$(hiddenInput3).val(checkedAnswers[2]);
// Fire Expression Manager
checkconditions(hiddenInput.value, hiddenInput.name, hiddenInput.type);
checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type);
checkconditions(hiddenInput3.value, hiddenInput3.name, hiddenInput3.type);
});
});
</script>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 11 months ago - 7 years 11 months ago #138127 by tpartner
This (untested) script should load the value of the "Other" text input. Note though, it does not provide for any randomization - it simply loads the first three checked items.

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 qHidden2 = thisQuestion.nextAll('.text-short:eq(1)');
    var qHidden3 = thisQuestion.nextAll('.text-short:eq(2)');
    var hiddenInput = $('input.text', qHidden);
    var hiddenInput2 = $('input.text', qHidden1);
    var hiddenInput3 = $('input.text', qHidden2);
 
    // Hide qHidden
    qHidden.hide();
    qHidden2.hide();
    qHidden3.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()));
        }        
      });
 
      // Load the hidden question with a random item from the array
      $(hiddenInput).val(checkedAnswers[0]);
      $(hiddenInput2).val(checkedAnswers[1]);
      $(hiddenInput3).val(checkedAnswers[2]);
 
      // Fire Expression Manager
      checkconditions(hiddenInput.value, hiddenInput.name, hiddenInput.type);
      checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type);
      checkconditions(hiddenInput3.value, hiddenInput3.name, hiddenInput3.type);
    }
    });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 11 months ago by tpartner.
The topic has been locked.
More
7 years 11 months ago #138128 by Juanoche
That's exactly what I want and seems to works fine. Thanks tpartner!!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose