- Posts: 20
- Thank you received: 6
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
- rauno_s
- Topic Author
- Offline
- Junior Member
Less
More
8 years 3 months ago #129008
by rauno_s
Using/saving a random element from multiple choice was created by 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!
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!
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
8 years 3 months ago #129012
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using/saving a random element from multiple choice
What LimeSurvey version?
Can you point us to the javascript "official" workaround?
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.
The topic has been locked.
- rauno_s
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
- Thank you received: 6
8 years 3 months ago #129018
by rauno_s
Replied by rauno_s on topic Using/saving a random element from multiple choice
Workaround -
manual.limesurvey.org/Workarounds:_Manip...estion_for_later_use
Version 2.06+ Build 150930
Version 2.06+ Build 150930
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
8 years 3 months ago #129021
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using/saving a random element from multiple choice
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.
Demo survey attached:
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.
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: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:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: rauno_s
The topic has been locked.
- rauno_s
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
- Thank you received: 6
7 years 11 months ago #133570
by rauno_s
Replied by rauno_s on topic Using/saving a random element from multiple choice
thank you very much, works excellent!
The topic has been locked.
- Juanoche
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
7 years 9 months ago #137707
by Juanoche
Replied by Juanoche on topic Using/saving a random element from multiple choice
I imported your demo survey but it doesn't work for me. Any suggestion?
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
7 years 9 months ago #137714
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using/saving a random element from multiple choice
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.
The topic has been locked.
- Juanoche
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
7 years 9 months ago #137721
by Juanoche
Replied by Juanoche on topic Using/saving a random element from multiple choice
Sorry I'm using Versión 2.50+ Build 160526
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
7 years 9 months ago #137754
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using/saving a random element from multiple choice
This script should work for LS version 2.5:
Sample survey attached:
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:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
- Juanoche
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 0
7 years 9 months ago #137809
by Juanoche
Replied by Juanoche on topic Using/saving a random element from multiple choice
Thanks, It works fine
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10612
- Thank you received: 3691
7 years 9 months ago #137817
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using/saving a random element from multiple choice
I updated the workaround for 2.5 -
manual.limesurvey.org/Workarounds:_Manip...estion_for_later_use
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- Deusdeorum
- Offline
- Senior Member
Less
More
- Posts: 72
- Thank you received: 17
7 years 9 months ago #137851
by Deusdeorum
Replied by Deusdeorum on topic Using/saving a random element from multiple choice
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.
The topic has been locked.