- Posts: 117
- Thank you received: 9
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Randomly pick a option from a hidden multi-choice question
- paulfiner
- Topic Author
- Offline
- Premium Member
Less
More
3 years 3 days ago #215215
by paulfiner
Randomly pick a option from a hidden multi-choice question was created by paulfiner
I have a list of 20 brands that respondents can say whether they've heard of or not in a multi-choice question.
I am only interested in 3 of these brands which I have set up an equation to populate a hidden multi-choice question for.
If only 1 of the 3 brands is mentioned I need to use it in a following question.
If 2 or 3 brands are mentioned I need to randomly pick one of them to use later on.
If none of the brands are mentioned I need to pick a random one from all 3.
I'm using v3.25 and have seen the various workarounds in the forum and manual but am struggling to work out the logic.
Thanks,
Paul
I am only interested in 3 of these brands which I have set up an equation to populate a hidden multi-choice question for.
If only 1 of the 3 brands is mentioned I need to use it in a following question.
If 2 or 3 brands are mentioned I need to randomly pick one of them to use later on.
If none of the brands are mentioned I need to pick a random one from all 3.
I'm using v3.25 and have seen the various workarounds in the forum and manual but am struggling to work out the logic.
Thanks,
Paul
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
3 years 3 days ago #215216
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 Randomly pick a option from a hidden multi-choice question
It could be done with JS. Can you attach a small sample survey (.lss file) containing only the relevant questions?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- paulfiner
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 117
- Thank you received: 9
3 years 3 days ago - 3 years 3 days ago #215218
by paulfiner
Replied by paulfiner on topic Randomly pick a option from a hidden multi-choice question
Hi Tony,
Here's the lss file. I just need to populate the short textfield with the chosen brand.
Cheers,
Paul
Here's the lss file. I just need to populate the short textfield with the chosen brand.
Cheers,
Paul
Last edit: 3 years 3 days ago by paulfiner.
The topic has been locked.
- jelo
- Offline
- Platinum Member
Less
More
- Posts: 5033
- Thank you received: 1257
3 years 3 days ago #215220
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic Randomly pick a option from a hidden multi-choice question
paulfiner wrote: I have a list of 20 brands that respondents can say whether they've heard of or not in a multi-choice question.
I am only interested in 3 of these brands which I have set up an equation to populate a hidden multi-choice question for.
If only 1 of the 3 brands is mentioned I need to use it in a following question.
If 2 or 3 brands are mentioned I need to randomly pick one of them to use later on.
If none of the brands are mentioned I need to pick a random one from all 3.
I'm using v3.25 and have seen the various workarounds in the forum and manual but am struggling to work out the logic.
Thanks,
Paul
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
3 years 3 days ago #215223
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 Randomly pick a option from a hidden multi-choice question
Place this script in the source of the first multiple-choice. It will:
- place a listener on the check-boxes in that question
- randomize the checked target codes as you describe above
- load the short-text with the selected random code
- check the corresponding box in the second multiple-choice (I change the codes in that question so they match the first question)
Sample survey attached:
- place a listener on the check-boxes in that question
- randomize the checked target codes as you describe above
- load the short-text with the selected random code
- check the corresponding box in the second multiple-choice (I change the codes in that question so they match the first question)
Code:
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify the questions var qID = '{QID}'; var thisQuestion = $('#question'+qID); var textQuestion = thisQuestion.nextAll('.text-short:eq(0)'); var nextMultiOpt = thisQuestion.nextAll('.multiple-opt:eq(0)'); var nMOID = nextMultiOpt.attr('id').replace(/question/, ''); // Identify the sub-question codes to target var sqCodes = [4, 5, 9]; // Listener on the checkboxes $(':checkbox', thisQuestion).on('change', function(e) { // Reset the hidden questions $(':text', textQuestion).val(''); $(':checkbox', nextMultiOpt).prop('checked', false).trigger('change'); // Build an array of checked codes (use all if none are checked) var checkedCodes = []; $.each(sqCodes, function(i, val) { if($(':checkbox[id$="X'+qID+val+'"]').is(':checked')) { checkedCodes.push(val); } }); var randomCodes = sqCodes; if(checkedCodes.length > 0) { randomCodes = checkedCodes; } // Randomize the codes shuffleArray(randomCodes); // Load the hidden questions var selectedCode = randomCodes[0]; $(':text', textQuestion).val(selectedCode); $(':checkbox[id$="X'+nMOID+selectedCode+'"]', nextMultiOpt).prop('checked', true).trigger('change'); }); }); 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:
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: paulfiner
The topic has been locked.
- paulfiner
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 117
- Thank you received: 9
3 years 3 days ago #215224
by paulfiner
Replied by paulfiner on topic Randomly pick a option from a hidden multi-choice question
Hi Tony,
Thank you so much for helping out.
I’ve left work now but will implement this in the morning.
Cheers
Paul
Thank you so much for helping out.
I’ve left work now but will implement this in the morning.
Cheers
Paul
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12916
- Thank you received: 3975
3 years 3 days ago #215225
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Randomly pick a option from a hidden multi-choice question
Hi,
Of course Tony provides the awesome javascript.
As I am not able to write one line of javascript by myself, my speciality are equations.
This is just to show that it is possible with only three equations.
L'art pour l'art
Joffm
Of course Tony provides the awesome javascript.
As I am not able to write one line of javascript by myself, my speciality are equations.
This is just to show that it is possible with only three equations.
L'art pour l'art
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: paulfiner
The topic has been locked.
- paulfiner
- Topic Author
- Offline
- Premium Member
Less
More
- Posts: 117
- Thank you received: 9
3 years 3 days ago #215230
by paulfiner
Replied by paulfiner on topic Randomly pick a option from a hidden multi-choice question
Hi Joffm,
Thanks for contributing to my problem and that's a neat solution for sure.
I've got some studying to do!
Cheers,
Paul
Thanks for contributing to my problem and that's a neat solution for sure.
I've got some studying to do!
Cheers,
Paul
The topic has been locked.