Welcome to the LimeSurvey Community Forum

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

problem with script array

  • Maribelcarol
  • Maribelcarol's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209125 by Maribelcarol
problem with script array was created by Maribelcarol
Hi again! So I was able to more or less get the script for an array I wanted, but there's some problem with the section of questions. If I preview each question separated, everything seems fine, though if I run the survey or I preview it completely something bugs and I cannot understand what happens. The survey is attached in case somebody wants to check the script. Thanks a lot in advance.

File Attachment:

File Name: limesurvey...6864.lss
File Size:68 KB
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago - 3 years 4 months ago #209141 by tpartner
Replied by tpartner on topic problem with script array
You need to assign a scope "thisQuestion" for the elements so their insertion is not repeated.

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Assign column-specific classes
    $('table.subquestion-list tr', thisQuestion).each(function(i) {
      $('> *:gt(0)', this).each(function(i){
        $(this).addClass('column-'+(i+1));
        $(this).attr('data-column', i+1);
      });
    });
 
    // Hide the text inputs in columns 1 and 4
    $('.column-1 input[type="text"]', thisQuestion).hide();
    $('.column-2 input[type="text"]', thisQuestion).hide();
    $('.column-3 input[type="text"]', thisQuestion).hide();
    $('.column-4 input[type="text"]', thisQuestion).hide();
    $('.column-5 input[type="text"]', thisQuestion).hide();
    $('.column-6 input[type="text"]', thisQuestion).hide();
 
    // Insert the checkboxes into column 1
    $('.answer-item.column-1', thisQuestion).append('<input class="checkbox inserted-checkbox" type="checkbox" />');
 
    $('.answer-item.column-2', thisQuestion).append('<input class="checkbox inserted-checkbox" type="checkbox" />');
    $('.answer-item.column-3', thisQuestion).append('<input class="checkbox inserted-checkbox" type="checkbox" />');
    $('.answer-item.column-4', thisQuestion).append('<input class="checkbox inserted-checkbox" type="checkbox" />');
    $('.answer-item.column-5', thisQuestion).append('<input class="checkbox inserted-checkbox" type="checkbox" />');
    $('.answer-item.column-6', thisQuestion).append('<input class="checkbox inserted-checkbox" type="checkbox" />');
 
    // Initial checkbox states (if the question has already been answered)
    $('.inserted-checkbox', thisQuestion).each(function(i) {
      if($.trim($(this).closest('td').find('input[type="text"]:eq(0)').val()) == 'Y') {
        $(this).prop('checked', true);
      }
    });
 
    // Listener on the checkboxes (insert "Y" into hidden text input when checked)
    $('.inserted-checkbox', thisQuestion).change(function() {
      if($(this).is(':checked')) {
        $(this).closest('td').find('input[type="text"]').val('Y');
      }
      else {
        $(this).closest('td').find('input[type="text"]').val('');
      }
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 4 months ago by tpartner.
The topic has been locked.
  • Maribelcarol
  • Maribelcarol's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago #209142 by Maribelcarol
Replied by Maribelcarol on topic problem with script array
Thanks! Though I don't know how I am supposed to that. I 've been trying to search it but it doesn't work. Thanks a lot in advance.
The topic has been locked.
  • Maribelcarol
  • Maribelcarol's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 4 months ago - 3 years 4 months ago #209145 by Maribelcarol
Replied by Maribelcarol on topic problem with script array
I realised where the problem was! thank you so much!!!
Last edit: 3 years 4 months ago by Maribelcarol.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 4 months ago #209187 by Joffm
Replied by Joffm on topic problem with script array
Hi,
I do not understand, why you use a multiple question here.
It is single punch.
Why not just this?
Your former approach with radio buttons.

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify the questions
    var qArrayID = {QID};
    var qArray = $('#question'+qArrayID);
    var qMultiText = qArray.nextAll('.multiple-short-txt:eq(0)');
 
      // Hide the multi-short-text question
    $(qMultiText).hide();
        $('table.questions-list col:not(.col-answers)', qArray).css('width', 'auto');
 
 
      $('table.questions-list thead tr', qArray).append('<th class="answer-text inserted-column-label" /></th>');
//      $('table.questions-list thead tr th.answer-text.inserted-column-label', qArray).css('width','40%');
 
    $('tr.answers-list', qArray).each(function(i) {
      $(this).append('<td class="answer-item text-item">\
              </td>\
              ');
    });
 
    // Load the column label for the text inputs
    $('.inserted-column-label:eq(0)', qArray).text($('.ls-label-question', qMultiText).text());
    $('.text-item', qArray).append('<label class="ls-label-xs-visibility">'+$('.ls-label-question', qMultiText).text()+'</label>');
 
    // Loop through the multi-short-text sub-questions
    $('li.answer-item', qMultiText).each(function(i) {
      // Move the text inputs into the array
      $('input[type="text"]', this).appendTo($('tr.answers-list:eq('+i+') .text-item', qArray));
    });
 
  });
</script><script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
    // Add a question class
    thisQuestion.addClass('custom-array');
    // Column-specific classes
    $('table.subquestion-list tr', thisQuestion).each(function(i) {
      $('th, td', this).each(function(i) {
        $(this).addClass('column-'+i);
      });
    });
  });
</script>
Code:
<style type="text/css">.custom-array table.subquestion-list col {
    width: auto !important;
  }
 
  .custom-array table.subquestion-list thead .column-0 {  width: 28%; }
  .custom-array table.subquestion-list thead .column-1 {  width: 7%; }
  .custom-array table.subquestion-list thead .column-2 {  width: 7%; }
  .custom-array table.subquestion-list thead .column-3 {  width: 7%; }
  .custom-array table.subquestion-list thead .column-4 {  width: 7%; }
  .custom-array table.subquestion-list thead .column-5 {  width: 7%; }
  .custom-array table.subquestion-list thead .column-6 {  width: 7%; }
  .custom-array table.subquestion-list thead .column-7 {  width: 30%; }
</style>

The first group - G1a - shows the new idea.

File Attachment:

File Name: limesurvey...5941.lss
File Size:37 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose