- Posts: 169
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
how to random decide a question display or hide?
- iscar
- Topic Author
- Offline
- Elite Member
Less
More
9 years 7 months ago #112626
by iscar
how to random decide a question display or hide? was created by iscar
version 2.0,i need to design a survey with 10 questions, in which 9 questions are randomly display to participant.
it means participant must answer 1 question, final answer question number is from 1-10.
how to make it?
it means participant must answer 1 question, final answer question number is from 1-10.
how to make it?
The topic has been locked.
- iscar
- Topic Author
- Offline
- Elite Member
Less
More
- Posts: 169
- Thank you received: 0
9 years 3 weeks ago #119049
by iscar
Replied by iscar on topic how to random decide a question display or hide?
tha's also a question in 2.05
The topic has been locked.
- Bigred01
- Offline
- Premium Member
Less
More
- Posts: 88
- Thank you received: 33
9 years 3 weeks ago #119053
by Bigred01
Replied by Bigred01 on topic how to random decide a question display or hide?
Note: After re-reading your post like 20 times I'm confident I don't really understand the question. But in my haste, I created an answer that I'm sure wont help you in any way but I really don't want to delete it as it may (hopefully) help someone somewhere... I should probably go to bed...
.........................
On the first page where they all answer the same question, add a multiple text question with 9 answers.
Add this script to the page..
The example above has an array of 16 numbers. These numbers represent 16 potential question we might ask the respondent. The script randomizes the array of numbers, then stores the first 9 random numbers in the 9 text fields you added. You can then use relevance on each question to show if any of the 9 text boxes contains the number that represents that question.
.........................
On the first page where they all answer the same question, add a multiple text question with 9 answers.
Add this script to the page..
Code:
var questionArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; shuffleArray(questionArr); $(document).ready(function() { for(var i = 0;i<10;i++) { $('.text:eq('+i+')').val(questionArr[i]); } }); 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; }
The example above has an array of 16 numbers. These numbers represent 16 potential question we might ask the respondent. The script randomizes the array of numbers, then stores the first 9 random numbers in the 9 text fields you added. You can then use relevance on each question to show if any of the 9 text boxes contains the number that represents that question.
The following user(s) said Thank You: iscar
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10115
- Thank you received: 3597
9 years 3 weeks ago #119063
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 how to random decide a question display or hide?
Hmm...I seem to recognize that code...
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- Bigred01
- Offline
- Premium Member
Less
More
- Posts: 88
- Thank you received: 33
9 years 3 weeks ago #119088
by Bigred01
Replied by Bigred01 on topic how to random decide a question display or hide?
Ye Old Fisher Yates shuffle (copied from a stackoverflow post)
The topic has been locked.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11681
- Thank you received: 2748
9 years 3 weeks ago #119092
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic how to random decide a question display or hide?
Well, I am also not quite sure how to understand the question.
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10115
- Thank you received: 3597
9 years 3 weeks ago - 9 years 3 weeks ago #119094
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 how to random decide a question display or hide?
I think that Bigred has put his finger on it, although my approach would be slightly different.
Let's say, for a simpler example, that you wanted to randomly display 4 out of a set of 10 questions.
Somewhere preceding the set of 10, I would place a multiple-short-text question with 10 sub-questions - one for each of the set.
In that multiple-short-text, I place the script below which shuffles an array and populates a random 4 of the sub-questions with "1". Then relevance is used to only show the questions in the set of 10 that correspond to sub-questions that equal 1.
A sample survey:
Let's say, for a simpler example, that you wanted to randomly display 4 out of a set of 10 questions.
Somewhere preceding the set of 10, I would place a multiple-short-text question with 10 sub-questions - one for each of the set.
In that multiple-short-text, I place the script below which shuffles an array and populates a random 4 of the sub-questions with "1". Then relevance is used to only show the questions in the set of 10 that correspond to sub-questions that equal 1.
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Hide this question $('#question{QID}').hide(); // An array defining total number of questions in the set and number to be shown // In this case, 4 of 10 will be shown var setArray = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]; // Shuffle the array setArray = $.shuffle(setArray); // Loop Through the array and populate the sub-questions $(setArray).each(function(i, value) { if($('#question{QID} input.text:eq('+i+')').val() == '') { $('#question{QID} input.text:eq('+i+')').val(value); } }); }); // A function to shuffle arrays (function($){ $.fn.shuffle = function() { return this.each(function(){ var items = $(this).children(); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); return arr; } })(jQuery); </script>
A sample survey:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 9 years 3 weeks ago by tpartner.
The following user(s) said Thank You: iscar
The topic has been locked.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11681
- Thank you received: 2748
9 years 3 weeks ago #119095
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic how to random decide a question display or hide?
Works great Tony.
However, somehow the question still shows upon loading for the split of a second.
Is there any way to avoid this behaviour?
However, somehow the question still shows upon loading for the split of a second.
Is there any way to avoid this behaviour?
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
The topic has been locked.
- Bigred01
- Offline
- Premium Member
Less
More
- Posts: 88
- Thank you received: 33
9 years 3 weeks ago #119096
by Bigred01
Replied by Bigred01 on topic how to random decide a question display or hide?
Hiding it with css would prevent that.
I like seeing your js posts Tpartner. Always learn something.
I like seeing your js posts Tpartner. Always learn something.
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13676
- Thank you received: 2496
9 years 3 weeks ago #119140
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on topic how to random decide a question display or hide?
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.