Welcome to the LimeSurvey Community Forum

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

JS for selecting two random responses from a multiple choice question

  • dieterdubinsky
  • dieterdubinsky's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago - 7 years 4 months ago #143471 by dieterdubinsky
Hello again guys :) another thank you to Tony for helping me with this. I actually found a solution to take my two hidden text field variables and put them into two hidden single choice questions which can then be used for quotas (if anyone else is looking for this).

I hope I am allowed to post an external link here. I found this script: www.aptigence.com.au/wordpress/category/limesurvey/

which is actually based on numerical inputs but can be changed to text inputs. So, this is what I put into both of the single choice questions (Please note that they have to be on a different page than the hidden text questions (e.g. in another group in group-by-group-mode) and as far as I can tell there has to be some kind of visible question on the same group, but I'm not sure about that):
Code:
$(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>

For this, the single choice question answer codes have to be A, B, C, D. Credits of course go to jcleeland from Aptigence.

This is still not ideal as both randomly selected items are on two different questions so I kind of have to guess the quota behind each one. I'll doctor around some more in hopes of finding a solution for a single MC question that checks both answers for use in quotas.
Last edit: 7 years 4 months ago by dieterdubinsky.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago - 7 years 4 months ago #143474 by tpartner
To facilitate placing everything on a single page, here is an extension of my array workaround above with an added hidden multiple-choice question.

The multiple-choice question must be placed directly after the hidden multi-text and must have the same sub-questions and codes as the array question. The check-boxes corresponding the the hidden random texts will be checked.

Code:
<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>

Sample question attached:

File Attachment:

File Name: limesurvey...4421.lss
File Size:25 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 4 months ago by tpartner.
The topic has been locked.
  • dieterdubinsky
  • dieterdubinsky's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #143479 by dieterdubinsky
Wow! Again, I can't thank you enough. I just copied that over to a sample survey and it worked like a charm.

If it's not too much to ask: Could that script also be rewritten to also work for an MC question instead of an array? I know I had it the other way round before, but all the structures and relevance equations are now build upon this single MC question where the randomization is pulled from. If it is too much to ask I of course understand, I would then try to rebuild most of the survey with that array question as base.

It would look like this: MC question where the participant checks the items he knows --> two hidden text questions with two randomly selected items of those he knows --> hidden MC question that checks both items given in the text questions.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago - 7 years 4 months ago #143481 by tpartner
Yep, that would be like this:

Code:
<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>

Sample survey attached:

File Attachment:

File Name: limesurvey...21-2.lss
File Size:24 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 4 months ago by tpartner.
The topic has been locked.
  • dieterdubinsky
  • dieterdubinsky's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #143487 by dieterdubinsky
Tony, you are a genius! I hope this was not too much trouble :-). Everything is working perfectly, I couldn't ask for more.

I hope this helps others, too. Selecting random responses from a list of "known" items is very common in e.g. brand studies - so it is a frequently used feature in a lot of surveys.
The topic has been locked.
More
4 years 2 weeks ago #194635 by arnabbhuyan
Hi,
I tried to use the .lss file to test. But unfortunately it seems the code is not working. Even I tried to unhide "qhidden" and the values are not getting punched in the question. Can you please help me on this.

Thanks,
AB
Attachments:
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 weeks ago #194638 by tpartner
I'm not surprised it doesn't work after more than 3 years.

What LimeSurvey version are you using?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu
The topic has been locked.
More
4 years 2 weeks ago #194641 by arnabbhuyan
Hi,
Version 3.16.1+190314

Thanks,
AB
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 weeks ago #194648 by tpartner
This will work in versions 3.x and 4.x:

Code:
<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>

Sample survey attached:

File Attachment:

File Name: limesurvey...2-27.lss
File Size:26 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu
The topic has been locked.
More
4 years 2 weeks ago #194845 by arnabbhuyan
Thanks Man!!
It works.

Can you also let me know which code will work for 2.05 shared in this forum?
And what changes you made in the code to work for 3.0 or above?

Thanks,
AB
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 weeks ago #194852 by tpartner
Sorry, I don't have time for that. You can search the forum and compare scripts as easily as I can.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
4 years 2 weeks ago #194859 by arnabbhuyan
Thanks for you time :)
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose