Welcome to the LimeSurvey Community Forum

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

Can't get my 2.x MaxDiff script to work after upgrading to Version 3.13.2+180709

  • Siem
  • Siem's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 8 months ago - 5 years 8 months ago #171525 by Siem
I have upgraded LimeSurvey from 2.x to Version 3.13.2+180709
After fixing the theme, my javascripts stopped working. One of the scripts is based on MaxDiff manual.limesurvey.org/Workarounds:_Quest...axDiff_question_type

It prevents participants from selecting ‘most’ and ‘least’ for the same statement.
a question looks more or less like this:
..................................most.....least
"I like fishing"................o..........o
"I like driving"................o..........o
"I like sleeping".............o..........o

The javascript code:
Code:
function maxDiff(qID, randomize) {
 
  // Identify some elements
  var thisQuestion = $('#question'+qID);
  var thisTable = $('table.subquestion-list:eq(0)', thisQuestion);
 
  // Assign a new question class
  $(thisQuestion).addClass('max-diff-array');
 
  // Move the columns
  // $('thead tr:eq(0)', thisTable).prepend($('thead tr:eq(0) th:eq(1)', thisTable));
  //$('tr.answers-list', thisTable).each(function(i){
  //  $('td.answer-item:eq(0)', this).prependTo(this);
  //});
 
  // Random rows
  if(randomize) {
    var rowsArr = [];
    $('tr.answers-list', thisTable).each(function(i){
      $(this).attr('data-index', i);
      rowsArr.push(i);
    });
    shuffleArray(rowsArr);
    $(rowsArr).each(function(i){
      $('tbody', thisTable).append($('tr[data-index="'+this+'"]', thisTable));
    });
  }
 
  // Prevent clicking twice in the same row
  $('input.radio', thisQuestion).on('click', function () {
 
    $('input.radio', thisQuestion).prop('disabled', false);
    $('input.radio:checked', thisQuestion).each(function(i) {
      var thisRow = $(this).closest('tr.answers-list');
      $('input.radio', thisRow).not(this).prop('disabled', true);
    });
  });
 
  // Fix up the row classes
  var rowClass = 1;
  $('tr.answers-list', thisTable).each(function(i) {
    $(this).addClass('array'+(2-(i%2)));
  });
}
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;
}


This is in the body of the question:
Code:
<script type="text/javascript" charset="utf-8">    
$(document).ready(function(){
// Call the maxDiff() function
// Set the second parameter to true for randomized rows
maxDiff({QID}, false);
});
</script>

How can I make this work again in Version 3.13.2+180709 ?

Many thanks in advance for helping
Last edit: 5 years 8 months ago by Siem.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 8 months ago - 5 years 8 months ago #171527 by Joffm
Hi,

you know from your other snippet, that in LS 3.x. you have to change "input.radio" to "input[type="radio"]".

So I just did that and saw this commented lines in your script, which I uncommented:
Code:
// $('thead tr:eq(0)', thisTable).prepend($('thead tr:eq(0) th:eq(1)', thisTable));
  //$('tr.answers-list', thisTable).each(function(i){
  //  $('td.answer-item:eq(0)', this).prependTo(this);
  //});

Then I put everything into the question text and got this:


Not to bad; the rest of some css is up to you.
Code:
<script type="text/javascript" charset="utf-8">     
function maxDiff(qID, randomize) {
 
  // Identify some elements
  var thisQuestion = $('#question'+qID);
  var thisTable = $('table.subquestion-list:eq(0)', thisQuestion);
 
  // Assign a new question class
  $(thisQuestion).addClass('max-diff-array');
 
  // Move the columns
   $('thead tr:eq(0)', thisTable).prepend($('thead tr:eq(0) th:eq(1)', thisTable));
  $('tr.answers-list', thisTable).each(function(i){
  $('td.answer-item:eq(0)', this).prependTo(this);
  });
 
  // Random rows
  if(randomize) {
    var rowsArr = [];
    $('tr.answers-list', thisTable).each(function(i){
      $(this).attr('data-index', i);
      rowsArr.push(i);
    });
    shuffleArray(rowsArr);
    $(rowsArr).each(function(i){
      $('tbody', thisTable).append($('tr[data-index="'+this+'"]', thisTable));
    });
  }
 
  // Prevent clicking twice in the same row
  $('input[type="radio"]', thisQuestion).on('click', function () {
 
    $('input[type="radio"]', thisQuestion).prop('disabled', false);
    $('input[type="radio"]:checked', thisQuestion).each(function(i) {
      var thisRow = $(this).closest('tr.answers-list');
      $('input[type="radio"]', thisRow).not(this).prop('disabled', true);
    });
  });
 
  // Fix up the row classes
  var rowClass = 1;
  $('tr.answers-list', thisTable).each(function(i) {
    $(this).addClass('array'+(2-(i%2)));
  });
}
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;
}
 
  $(document).ready(function(){
    // Call the maxDiff() function
    // Set the second parameter to true for randomized rows
    maxDiff({QID}, true);
  });
</script>

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 5 years 8 months ago by Joffm.
The following user(s) said Thank You: tpartner, Siem
The topic has been locked.
  • Siem
  • Siem's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
5 years 8 months ago #171665 by Siem
Hi Joff, it works as expected now :) Many thanks!

Is there a way to reformat the code below to make it work in a custom.js file instead of the question body? I tried removing the script tags and puttin it in custom.js
Code:
<script type="text/javascript" charset="utf-8">     
function maxDiff(qID, randomize) {
 
  // Identify some elements
  var thisQuestion = $('#question'+qID);
  var thisTable = $('table.subquestion-list:eq(0)', thisQuestion);
 
  // Assign a new question class
  $(thisQuestion).addClass('max-diff-array');
 
  // Move the columns
   $('thead tr:eq(0)', thisTable).prepend($('thead tr:eq(0) th:eq(1)', thisTable));
  $('tr.answers-list', thisTable).each(function(i){
  $('td.answer-item:eq(0)', this).prependTo(this);
  });
 
  // Random rows
  if(randomize) {
    var rowsArr = [];
    $('tr.answers-list', thisTable).each(function(i){
      $(this).attr('data-index', i);
      rowsArr.push(i);
    });
    shuffleArray(rowsArr);
    $(rowsArr).each(function(i){
      $('tbody', thisTable).append($('tr[data-index="'+this+'"]', thisTable));
    });
  }
 
  // Prevent clicking twice in the same row
  $('input[type="radio"]', thisQuestion).on('click', function () {
 
    $('input[type="radio"]', thisQuestion).prop('disabled', false);
    $('input[type="radio"]:checked', thisQuestion).each(function(i) {
      var thisRow = $(this).closest('tr.answers-list');
      $('input[type="radio"]', thisRow).not(this).prop('disabled', true);
    });
  });
 
  // Fix up the row classes
  var rowClass = 1;
  $('tr.answers-list', thisTable).each(function(i) {
    $(this).addClass('array'+(2-(i%2)));
  });
}
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;
}
 
  $(document).ready(function(){
    // Call the maxDiff() function
    // Set the second parameter to true for randomized rows
    maxDiff({QID}, true);
  });
</script>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 8 months ago #171694 by tpartner
You can place most of it in custom.js but, since no Expression Manager variables are available there, you will still need to place the call for the function (the part inside the $(document).ready(function()) in a question.

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