Welcome to the LimeSurvey Community Forum

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

Combining numerical input with list (radio) question type

  • rbnbchr
  • rbnbchr's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 3 weeks ago #258106 by rbnbchr
Please help us help you and fill where relevant:
Your LimeSurvey version: 3.17.1+190408
Own server or LimeSurvey hosting:
Survey theme/template:
==================

Hello everyone,

I'm trying to combine two question types, i.e. numerical input and list (radio), to create something similar to the following table.



I've already looked at javascript workarounds and other forum posts, but nothing has really helped so far.

I'm fairly new to Lime Survey and haven't really worked with javascript other than copying and pasting the bits of code I needed. Any help would be greatly appreciated.

Thanks in advance for any ideas!


 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 3 weeks ago #258115 by Joffm
Hi, 
I can offer this
 
Put a question of type "multiple short text" with as much subquestions as your array in the group after the array question
Insert the code into the array question (source code mode)
Code:
<script type="text/javascript" data-author="Tony Partner">
    
    $(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();
 
        // Remove the core column widths
        $('table.questions-list col:not(.col-answers)', qArray).css('width', 'auto');
 
        // Insert the header cells
        $('table.questions-list thead td:eq(0)', qArray).after('<th class="answer-text inserted-column-label" /></th>');
        $('.inserted-column-label', qArray).css('width','30%');
 
        // Insert the answer row cells
        $('tr.answers-list', qArray).each(function(i) {
            $('.answertext', this).after('<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());
 
        // 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>

You also could use an array(text) with a drop-down in the second column.

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • rbnbchr
  • rbnbchr's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 3 weeks ago #258120 by rbnbchr
Thank you very much, Joffm.

" You could also use an array(text) with a dropdown in the second column."

That actually sounds like a better idea. However, I assume I can't use the same code for that.

I have now an array(text) as the first question and list(dropdown) as the 2nd. What do I need to change in the javascript code?

Thanks again!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 3 weeks ago #258124 by Joffm

I have now an array(text) as the first question and list(dropdown) as the 2nd. What do I need to change in the javascript code?

No, this is a different approach.
You use only one question, an arrayText)
With the javascript you insert a dropdown into the second column.
 
Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X002', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
            <option value="">...</option>\
            <option value="1">Calculation</option>\
            <option value="2">Estimation</option>\
            <option value="3">Don\'t know</option>\
    </select>');
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($('option:selected', this).val()).trigger('change');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('change');
      }
    });
 
 
  // Returning to page
    $('.with-select input:text', thisQuestion).[url=http://www.php.net/each]each[/url](function(i) {
      var thisCell = $(this).closest('.answer-item');
      var inputText = $.[url=http://www.php.net/trim]trim[/url]($(this).val());
      $('select.inserted-select', thisCell).val(inputText);
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('.with-select input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

Here the code of the second column is "X002"

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 weeks ago #258195 by Joffm
Please, read here about the three different editor types

[url] manual.limesurvey.org/First_login_-_your...My_personal_settings [/url]

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 weeks ago #258270 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.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose