Using/saving a random element from multiple choice

Mehr
3 Jahre 8 Monate her #129008 von rauno_s
Hello forum,

is there a way with the Expression Manager to pull a random answer from a previous multiple choice answer? A mockup as follows:

Q1: Have you tried the following brands? (multi choice)
(x) Pepsi
( ) Coca
(x) Fanta

Q2: Please rate your experience with FANTA (brand name here is dynamic text field; fanta is randomly selected from Pepsi and Fanta)
( ) good
( ) bad
etc

Also I need to have the the shown brand name to be saved as a variable to the database. Yes, there is a javascript "official" workaround, but it does not seem to work with the newer versions of LS,

thanks for any feedback!

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

LimeSurvey Partners
Mehr
3 Jahre 8 Monate her #129012 von tpartner
What LimeSurvey version?

Can you point us to the javascript "official" workaround?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 8 Monate her #129018 von rauno_s

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 8 Monate her #129021 von tpartner
I don't know of a way to do it with Expression Manager but I have updated the workaround .

1) Set up your survey to use JavaScript .

2) Create the multiple options question.

3) Immediately following that question, create a short-text question to store the label of the checked box in (we'll hide this with JavaScript)

4) Place the following script in the source of the multiple options question.

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

Demo survey attached:

Dateianhang:

Dateiname: Demo_Rando...oice.lss
Dateigröße:17 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Anhänge:
Folgende Benutzer bedankten sich: rauno_s

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 4 Monate her #133570 von rauno_s
thank you very much, works excellent!

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137707 von Juanoche
I imported your Demo survey but it doesn't work for me. Any suggestion?

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137714 von tpartner
That workaround is for LimeSurvey 2.06. What version are you using?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137721 von Juanoche
Sorry I'm using Versión 2.50+ Build 160526

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137754 von tpartner
This script should work for LS version 2.5:

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

Dateianhang:

Dateiname: limesurvey...1461.lss
Dateigröße:18 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Anhänge:

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137809 von Juanoche
Thanks, It works fine

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137817 von tpartner

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #137851 von Deusdeorum
This can be done if you create a hidden equation and pass in values with EM if function and rand() function as well, then create your Q2 and use another IF function to show either Pepsi, Coca or Fanta based on the random values of the hidden equation question.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #138124 von Juanoche

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

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

Dateianhang:

Dateiname: limesurvey...1461.lss
Dateigröße: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>

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her - 3 Jahre 2 Monate her #138127 von 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.

<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.
Official LimeSurvey Partner - partnersurveys.com
Letzte Änderung: 3 Jahre 2 Monate her von tpartner.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Mehr
3 Jahre 2 Monate her #138128 von Juanoche
That's exactly what I want and seems to works fine. Thanks tpartner!!

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Jetzt loslegen!

Melden Sie sich jetzt an, und erstellen Sie in wenigen Minuten Ihre erste Umfrage.

Account einrichten

Abonnieren Sie unseren Newsletter

Abonnieren Sie unseren Newsletter für alle Neuigkeiten rund um LimeSurvey
captcha