Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Exclude specific answer from randomization

  • Thomas
  • Thomas's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 11 months ago - 11 years 11 months ago #79243 by Thomas
Dear all,

I am currently working on a questionnaire including a constant sum scale. That is, I have included a question of the type multiple numerical input. To reduce biases, I have activated random order.

However I also need to include an answer option "Others:". As multiple numerical input does not feature "other" functionality I have simply included one answer option "Others:". Unfortunately this gets included in the randomization.

So what I currently might get is:

Answer C: _____
Others: _____
Answer A: _____
Answer D: _____
Answer B: _____

What I would like to achieve is:

Answer C: _____
Answer A: _____
Answer D: _____
Answer B: _____
Others: _____

In brief: I would need to exclude one specific answer from randomization and statically set it as the final answer.

Is there any know way to achieve this?

Thanks for your help!

Thomas
Last edit: 11 years 11 months ago by Thomas.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
11 years 11 months ago #79272 by Mazi

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The following user(s) said Thank You: Thomas
The topic has been locked.
  • Thomas
  • Thomas's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 11 months ago - 11 years 11 months ago #79283 by Thomas
Replied by Thomas on topic Exclude specific answer from randomization
Mazi,

Thank you very much for pointing me in the right direction.

Even though my jQuery and JavaScript capabilities are really limited I have managed to modify the workarounds to achieve what I have been looking for.

For future reference I would integrate my solution into the wiki. However I believe the jQuery insertion I have chosen is rather specific and would not work if the advanced settings of the question would not contain an "equal_sum", as I had to insert the answer right before the "li.multiplenumerichelp" (see below) which I believe only appears in this specific case.

Does any of you with more jQuery/DOM experience have an idea how to generalize?

Anyway, thank you very much for the help - my current problem has been solved!
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() {
 
    // Function to allow randomization of all answers except the last one in multiple numeric input questions
    function partRand(sID, gID, qID) { 
 
      // Find the number of answers
      var ansCount = ''
      $( 'div#question' + qID + ' div.answers li' ).each(function(i) {
        ansCount = (i - 1);
      });
 
      // Place the last answer created at the end of the list - but before the first helping sum
      $( 'li#javatbd' + sID + 'X' + gID + 'X' + qID + ansCount + '' ).insertBefore($( 'div#question' + qID + ' div.answers li.multiplenumerichelp' ).first());
      }
 
    // Call the function with the SID, GID and QID
    partRand(SID, GID, QID);
 
  });
Last edit: 11 years 11 months ago by Thomas.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
11 years 11 months ago - 11 years 11 months ago #79411 by Mazi

Thomas wrote: For future reference I would integrate my solution into the wiki.

Please add your solution!

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
Last edit: 11 years 11 months ago by Mazi.
The topic has been locked.
  • Thomas
  • Thomas's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 11 months ago #79441 by Thomas
Replied by Thomas on topic Exclude specific answer from randomization
Ok, I have tried my very best and added a note regarding the specific requirements:
docs.limesurvey.org/Workarounds%3A+Manip...iple_numerical_input
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
11 years 11 months ago #79450 by Mazi
Great, thanks!

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
11 years 5 months ago - 11 years 5 months ago #86814 by paulalford
Replied by paulalford on topic Exclude specific answer from randomization

Mazi wrote: Have a look at these workarounds:
docs.limesurvey.org/Workarounds%3A+Manip...List_radio_questions

docs.limesurvey.org/Workarounds%3A+Manip...t_dropdown_questions


That works OK for one answer in a list, but can it be done for more than one. I have a list:

Photovoltaic (PV) Array to generate electricity
Wind Farm to generate electricity
Energy efficient lighting
Combined heat and power
Electric Vehicles
Energy efficient IT
Solar water heating
Improved insulation
Other
None
Don’t know

And these need to be randomized, except the last three in the list. Is that possible?
Last edit: 11 years 5 months ago by paulalford.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
11 years 5 months ago #86819 by Mazi
When using your own, custom code, this is possible as well, you just need to adjust the code accordingly.

To have multiple exclude other options, simply separate them with a ";" when entering the answer codes at the advanced question settings.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
11 years 5 months ago - 11 years 5 months ago #86885 by paulalford
Replied by paulalford on topic Exclude specific answer from randomization
Hi Mazi,

Often in market research studies we want to random order and have three options that shouldn't be randomized, for example, Other, None and Don't Know. This is the problem I encountered.

Below is the JavaScript that lets me randomize the list but forces the last three list options to be placed at the end. I will modify this script so we can specify how many list options we force to be un-randomized.

Randomize answer options needs to be turned off in Advanced Options.

Cheers
Paul
Code:
<script type="text/javascript">
 
$(document).ready(function() {
 
    // Function to allow randomization of all answers except the last three in Multiple options and List/radio questions
    function partRand(sID, gID, qID) { 
 
      // Find the number of answers
      var ansCount = ''
      lastthreeitems=new Array();
        var liid=new Array();
        var $ul;
        var j=0;
      $( '#question' + qID + ' td.answer li' ).each(function(i) {
              ansCount = (i + 1);              
 
});
 
$( '#question' + qID + ' td.answer li' ).each(function(i) {
              if(i>ansCount-4){              
                lastthreeitems[j]=$(this).html();  
                liid[j]=$(this).attr("id");
 
 
                j++;              
                $(this).remove();
               }                        
 
});
$( '#question' + qID + ' td.answer li' ).each(function(i) {    
// get current ul
 
 
 
 
$ul = $(this).parent();
// get array of list items in current ul
var $liArr = $ul.children('li');
// sort array of list items in current ul randomly
$liArr.sort(function(a,b){
// Get a random number between 0 and 100
var temp = parseInt( Math.random()*100 );
// Get 1 or 0, whether temp is odd or even
var isOddOrEven = temp%2;
// Get +1 or -1, whether temp greater or smaller than 5
var isPosOrNeg = temp>5 ? 1 : -1;
// Return -1, 0, or +1
return( isOddOrEven*isPosOrNeg );
})
// append list items to ul
.appendTo($ul);
 
});  
 
$.each(lastthreeitems,function(i){
 
$( '#question' + qID + ' td.answer ul' ).append("<li id="+liid[i]+">"+this+"</li>");
});
 
  }
 
    // Call the function with the SID, GID and QID
    partRand(SSSSS, GG, QQ);
 
  });
 
</script>
Last edit: 11 years 5 months ago by paulalford.
The topic has been locked.
More
11 years 5 months ago - 11 years 5 months ago #86914 by paulalford
Replied by paulalford on topic Exclude specific answer from randomization
Here is the updated Javascript to determine how many codes you exclude from the randomization. In the code example it is set to three. Just change the number of the var insertitems. I'll add this to the Wiki later too.
Code:
<script type="text/javascript">
 
$(document).ready(function() {
 
    // Function to allow randomization of all answers except the last one in Multiple options and List/radio questions
    function partRand(sID, gID, qID) { 
      //var to define how many lists to be added at the end
      var insertitems=3;
      // Find the number of answers
      var ansCount = ''
      lastitems=new Array();
        var liid=new Array();
        var $ul;
        var j=0;
      $( '#question' + qID + ' td.answer li' ).each(function(i) {
              ansCount = (i + 1);              
 
});
 
$( '#question' + qID + ' td.answer li' ).each(function(i) {
              if(i>ansCount-(insertitems+1)){              
                lastitems[j]=$(this).html();  
                liid[j]=$(this).attr("id");
 
 
                j++;              
                $(this).remove();
               }                        
 
});
$( '#question' + qID + ' td.answer li' ).each(function(i) {    
// get current ul
 
 
 
 
$ul = $(this).parent();
// get array of list items in current ul
var $liArr = $ul.children('li');
// sort array of list items in current ul randomly
$liArr.sort(function(a,b){
// Get a random number between 0 and 100
var temp = parseInt( Math.random()*100 );
// Get 1 or 0, whether temp is odd or even
var isOddOrEven = temp%2;
// Get +1 or -1, whether temp greater or smaller than 5
var isPosOrNeg = temp>5 ? 1 : -1;
// Return -1, 0, or +1
return( isOddOrEven*isPosOrNeg );
})
// append list items to ul
.appendTo($ul);
 
});  
 
$.each(lastitems,function(i){
 
$( $ul ).append("<li id="+liid[i]+">"+this+"</li>");
});
 
  }
 
    // Call the function with the SID, GID and QID
    partRand(SSSSS, GG, QQ);
 
  });
 
</script>
 
Last edit: 11 years 5 months ago by paulalford.
The topic has been locked.
More
5 years 11 months ago #166447 by stephanied
Replied by stephanied on topic Exclude specific answer from randomization
In version 2.73.1 I am trying to partially randomize a multiple numeric input question that has an equal sum value setting applied. I've attempted to manipulate the above code to work on this version, but my coding skills are subpar. Can anyone help me with editing this code? The question I am working on only has one fixed answer, but I would also like to be able to fix multiple answers in the future.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago #166451 by tpartner
Replied by tpartner on topic Exclude specific answer from randomization
That code is too bloated for your requirements.

Try this, modifying the "lastItems" values as required.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question ID
    var qID = '{QID}';
 
    // List the sub-question codes to be placed at the end of the list
    // (comma-separated)
    var lastItems = ['SQ007','SQ008'];
 
    // Insert the last items
    $.each(lastItems, function(i, val) {
      $('#question{QID} tr[id^="javatbd"]:last').after($('#question{QID} tr[id^="javatbd"][id$="X'+qID+val+'"]'));
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...3514.lss
File Size:18 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose