- Posts: 22
- Thank you received: 1
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Randomization order for array question
- Salini
- Topic Author
- Offline
- Junior Member
Less
More
3 years 8 months ago #203616
by Salini
Randomization order for array question was created by Salini
Hi,
I have a survey where I have applied randomization in an array question with 5 attributes.
I will need a variable where the randomization is being populated.
I came across a script but it's not working:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// Identify some stuff
var qHiddenID = {QID}; // The hidden question ID
var qHidden = $('#question'+qHiddenID); // The hidden question
var q1 = $('div.list-radio:eq(0)'); // First radio question on the page
var q1ID = $(q1).attr('id').split('question')[1]; // The first radio question ID
var gID = $('input:text', qHidden).attr('id').split('X')[1]; // The group ID
// Hide this question
$(qHidden).hide();
// Build an array of the Q1 answers
var answersArr = [];
$('input:radio', q1).each(function(i){
var answerID = $(this).attr('id').split('X'+gID+'X'+q1ID)[1];
answersArr.push(answerID);
});
// Load the array into the hidden question
$('input:text', qHidden).val(answersArr);
});
</script>
Can someone help please.
I'm using version 2.72.3.
Thanks a lot,
SLL
I have a survey where I have applied randomization in an array question with 5 attributes.
I will need a variable where the randomization is being populated.
I came across a script but it's not working:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// Identify some stuff
var qHiddenID = {QID}; // The hidden question ID
var qHidden = $('#question'+qHiddenID); // The hidden question
var q1 = $('div.list-radio:eq(0)'); // First radio question on the page
var q1ID = $(q1).attr('id').split('question')[1]; // The first radio question ID
var gID = $('input:text', qHidden).attr('id').split('X')[1]; // The group ID
// Hide this question
$(qHidden).hide();
// Build an array of the Q1 answers
var answersArr = [];
$('input:radio', q1).each(function(i){
var answerID = $(this).attr('id').split('X'+gID+'X'+q1ID)[1];
answersArr.push(answerID);
});
// Load the array into the hidden question
$('input:text', qHidden).val(answersArr);
});
</script>
Can someone help please.
I'm using version 2.72.3.
Thanks a lot,
SLL
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12940
- Thank you received: 3979
3 years 8 months ago #203619
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Randomization order for array question
Well, it's obvious that it doesn't work.
I have a survey where I have applied randomization in an array question with 5 attributes.
and
var q1 = $('div.list-radio:eq(0)'); // First radio question on the page
Joffm
I have a survey where I have applied randomization in an array question with 5 attributes.
and
var q1 = $('div.list-radio:eq(0)'); // First radio question on the page
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
- Salini
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 1
3 years 8 months ago #203620
by Salini
Replied by Salini on topic Randomization order for array question
Thank you for replying Joffm.
Can you please let me know how to correct the script?
Cheers,
Salini
Can you please let me know how to correct the script?
Cheers,
Salini
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12940
- Thank you received: 3979
3 years 8 months ago #203623
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Randomization order for array question
Hi,
I found this one, adapted a bit to arrays.
In this attached sample the text question that stores the order isn't hidden yet.
You have to do it by giving it the class "hidden"
Joffm
I found this one, adapted a bit to arrays.
Code:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ //Identify the questions var thisQuestion = $('#question{QID}'); var hiddenQuestion = $(thisQuestion).nextAll('.text-short:eq(0)'); // Create an array of answer codes var answerCodes = []; $('tr.answers-list', thisQuestion).each(function(i) { answerCodes.push($(this).attr('id').split('X{QID}')[1]); }); // Load the hidden question $('input:text', hiddenQuestion).val(answerCodes); }); </script>
In this attached sample the text question that stores the order isn't hidden yet.
You have to do it by giving it the class "hidden"
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
- Salini
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 1
3 years 8 months ago #203625
by Salini
Replied by Salini on topic Randomization order for array question
Hi Joffm,
Thank you a lot. I have tested and it's working.
The only problem is that my survey is a question by question survey as requested by the client.
When by group, it is populating the random order variable . But when it is question by question display, the random order variable is blank.
Is there a way to have same thing when display question by question?
Kind Regards,
Salini
Thank you a lot. I have tested and it's working.
The only problem is that my survey is a question by question survey as requested by the client.
When by group, it is populating the random order variable . But when it is question by question display, the random order variable is blank.
Is there a way to have same thing when display question by question?
Kind Regards,
Salini
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12940
- Thank you received: 3979
3 years 8 months ago #203628
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Randomization order for array question
Therefore we often recommend to design the survey as
"Each group contains one question" and display "group by group".
This way you have one question per page, but also the freedom to put two or more questions into one group.
This is sometimes desired to show two related questions on one screen, and sometimes necessary to use some workarounds with javascript (e.g. where you merge two questions into one, or like this)
Joffm
"Each group contains one question" and display "group by group".
This way you have one question per page, but also the freedom to put two or more questions into one group.
This is sometimes desired to show two related questions on one screen, and sometimes necessary to use some workarounds with javascript (e.g. where you merge two questions into one, or like this)
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: Salini
The topic has been locked.
- Salini
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 22
- Thank you received: 1
3 years 8 months ago #203629
by Salini
Replied by Salini on topic Randomization order for array question
Thank you so much Joffm
The topic has been locked.