Welcome to the LimeSurvey Community Forum

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

Multiple Subquestions that can be added at the time of survey Version 6.1.5+2306

More
1 year 3 months ago - 1 year 3 months ago #244983 by rrscw
Hello, 
I'm using an array question type where I have made the subquestion for entering name of a group of people while they can select their qualification from the radio buttons in the Answer Options. I'll attach a picture so you can get the idea of what I'm talking about. 
Now, since I want to add name and qualification of people in the survey, I want that the subquestion could be added at the time of survey like we add tasks in a to do list where we can add new task or delete it. Similarly, I want that the user can add names and qualification of all staff members.
If this cannot be implemented,
The number of entries to be added actually depends on a previous question where I am asking the number of staff members.
There is a maximum number of members. 

Even if I add subquestion with textbox for lets say 20 people but the entry done at the time of survey of staff members is only 10 then I want the other 10 extra subquestions to be hidden from user when they get to that part of survey. It should be according to the answer of the previous question asking the number of staff members.
Can any of this be done with Limesurvey. Is there a feature or add on that I can refer to ?
I'm using a self-hosted limesurvey that is offline.
This is my version : LimeSurvey Community Edition Version 6.1.5+230626
Please tell me if you need any other information.

 
Last edit: 1 year 3 months ago by rrscw.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago #244988 by Joffm
Hi,
maybe this is helpful
[url] forums.limesurvey.org/index.php/forum/de...o-add-new-answer-row [/url]

But you did not show us which script you used to insert the text boxes.

In such cases it is always the best to create a small sample of your work and send the lss export,
This helps us to understand better what you intend and what you did so far.

Joffm
 

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

Please Log in to join the conversation.

More
1 year 3 months ago #244994 by rrscw

 

​​​​Thank you for the very quick reply !
This is the code you provided.


$(document).ready(function() {
    // A function to add or remove rows of an Array (Multi Flexible)(Text) question
    function varLengthArray(qID) {
      if ($('#question'+qID+'').length > 0) {
        // The HTML content of the Add/Remove elements - modify as you wish
        var addContent = '[+] add row';
        var removeContent = '[-] remove row';
        // Create the Add and Remove elements & insert them
        var el1 = document.createElement('div');
        el1.setAttribute('id','addButton'+qID);
        el1.setAttribute('class','btn btn-primary');
        document.body.appendChild(el1);
        var el2 = document.createElement('div');
        el2.setAttribute('id','removeButton'+qID);
        el2.setAttribute('class','btn btn-primary');
        document.body.appendChild(el2);
        // Move them to after the array
        $( 'div#addButton'+qID ).appendTo($( '#question' + qID + ' table.ls-answers' ).parent());
        $( 'div#removeButton'+qID ).appendTo($( '#question' + qID + ' table.ls-answers' ).parent());
        // Insert their HTML
        $( 'div#addButton'+qID ).html( addContent );
        $( 'div#removeButton'+qID ).html( removeContent );
        // Style the elements - you can modify here if you wish
        $( 'div#addButton'+qID ).css({
          'margin':'10px 0 0 10px',
          'padding':'1px',
          'text-align':'center',
          'width':'auto',
          'cursor':'pointer',
          'float':'left'
        });
        $( 'div#removeButton'+qID ).css({
          'margin':'10px 0 0 10px',
          'padding':'1px',
          'text-align':'center',
          'width':'auto',
          'cursor':'pointer',
          'float':'left'
        });
        // Initially hide the Remove element
        $( 'div#removeButton'+qID ).hide();
        // Call the functions below when clicked
        $( 'div#addButton'+qID ).click(function (event) {
          addRow(qID);
        });
        $( 'div#removeButton'+qID ).click(function (event) {
          removeRow(qID);
        });
        // Function to add a row, also shows the Remove element and hides the
        //Add element if all rows are shown
        function addRow(qID) {
          var arrayRow = '#question' + qID + ' table.ls-answers tr.subquestion-list';
          var rowCount = $( arrayRow ).size() - 1;
          $( arrayRow + '[name="hidden"]:first' ).attr('name', 'visible').show();
          $( 'div#removeButton'+qID ).show();
          if ( $( arrayRow + ':eq(' + rowCount + ')' ).attr('name') == 'visible' )  {
            $( 'div#addButton'+qID ).hide();
          }
        }
        // Function to remove a row, also clears the contents of the removed row,
        // shows the Add element if the last row is hidden and hides the Remove
        // element if only the first row is shown
        function removeRow(qID) {
          var arrayRow = '#question' + qID + ' table.ls-answers tr.subquestion-list';
          var rowCount = $( arrayRow ).size() - 1;
          $( arrayRow + '[name="visible"]:last input[type="text"]' ).val('');
          $( arrayRow + '[name="visible"]:last' ).attr('name', 'hidden').hide();
          $( 'div#addButton'+qID ).show();
          if ( $( arrayRow + ':eq(1)' ).attr('name') == 'hidden' )  {
            $( 'div#removeButton'+qID ).hide();
          }
        }
        // Just some initialization stuff
        var arrayRow = '#question' + qID + ' table.ls-answers tr.subquestion-list';
        var rowCount = '';
        // Initially hide all except first row or any rows with populated inputs
        $( arrayRow ).each(function(i) {
          if ( i > 0 ) {
            // We also need to give the hidden rows a name cause IE doesn't
            // recognize jQuery :visible selector consistently
            $( this ).attr('name', 'hidden').hide();
            $('input[type=text]', this).each(function(i) {
              if ($(this).attr('value') != '') {
                $(this).parents('tbody:eq(0)').attr('name', 'visible').show();
                $( 'div#removeButton'+qID ).show();
              }
            });
            rowCount = i;
          }
        });
      }
    }
    // Call the function with a question ID
    varLengthArray({QID});
  });
</script>


I tried the code you provided in that link and also added the relevance equation and these are the issues.
  1. The relevance equation separately work fine, when one subquestion's radio button is clicked another question automatically appears.
  2. When I paste the code exactly. the buttons do appear. When add button is clicked the remove button appears as well. However, it does not actually add or remove any subquestions and the subquestions are just displaying the behavior I mentioned in first point. They are appearing when one subquestion is not empty due to the relevance eq and not the buttons themselves.
  3. when I change this line [varLengthArray({QID});] and add my question's id which is G04Q17, the buttons disappear as well.
You have introduced me to an even bigger problem by asking this question below, because after your response I made a sample survey and made another question in similar format, activated it and discovered that the textbox does not actually return any responses. I had only added this line [ <input type="textbox"> ] in my subquestion. That's why I didn't think I needed to add the code. I have attached a sample.

But you did not show us which script you used to insert the text boxes.

      

File Attachment:

File Name: limesurvey...7963.lss
File Size:29 KB

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago - 1 year 3 months ago #244995 by Joffm
Hi
This script is meant for array(text).

So stay to your solution to show the next row when one row is completed. (or adapt the script)

But as I asked before, how do you insert the text inputs.
As you do it now it will never work. How do you store the input?
But there is another example in the forum
[url] forums.limesurvey.org/index.php/forum/ca...ith-radiolist#223422 [/url]

Another idea:
Use an array(text)
First column you enter the text
Second column you display a dropdown with the options.

There are many exampüles in the forum how to insert dropdowns in an array(texct)


And:
Do NOT change {QID}
This is a Limesurvey variable that always contains the actual question ID.
What you entered is the question CODE, quite different

Joffm 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 year 3 months ago by Joffm.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago #244996 by Joffm
Here a screenshot of my last proposal.

Array(text) with dopdown cells, dynamic insertion of rows and customized column widths





 

File Attachment:

File Name: limesurvey...9631.lss
File Size:32 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: rrscw

Please Log in to join the conversation.

More
1 year 3 months ago - 1 year 3 months ago #245002 by rrscw
That works perfectly. !
I'm quite new to this and my survey wasn't active yet so I only realized that the textbox won't work after you pointed that out. I should've known.
Using the array text with dropdown is an excellent approach, I'm gonna use this one instead.
Thank you so much!
Last edit: 1 year 3 months ago by rrscw.

Please Log in to join the conversation.

More
1 year 3 months ago #245003 by rrscw
There's one more thing I wanted to ask. Can I restrict user from adding more rows after a certain number. Like, let's say they entered 5 total number of employees. Can I do something so that after entering name of 5 members they cannot add any using the add row button?

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago #245025 by tpartner
If the employee count question is on a previous page...

1) Place sub-question relevance on the array rows to only show them if the employee count is above a certain number.

2) Use this modified script to add/remove rows. Changes in the script are:
- Exclude the irrelevant rows from the add/remove functions
- Use add/remove buttons instead of <div> elements
- Add variables to handle the button classes and styles
- Reset the inserted drop-down(s) when a row is hidden
- Generally clean up code for better efficiency

Code:
<script type="text/javascript" data-author="Tony Partner">
  $(document).ready(function() {
 
     // A function to add or remove rows of an Array (Multi Flexible)(Text) question
    function varLengthArray(qID) {
 
      if ($('#question'+qID+'').length > 0) {
 
        var thisQuestion = $('#question'+qID);
 
        // The HTML content of the Add/Remove elements - modify as you wish
        var addContent = '[+] Add row';
        var removeContent = '[-] Remove row';
 
        // The classes for the Add/Remove elements - modify as you wish
        // See https://getbootstrap.com/docs/5.0/getting-started/introduction/
        var addClasses = 'inserted-button add btn btn-success';
        var removeClasses = 'inserted-button remove btn btn-danger';
 
        // The styles for the Add/Remove elements - modify as you wish
        // These could be placed in your custom.css file.
        var addStyles = 'margin:10px 0 10px 10px; padding:1px 5px; text-align:center; width:auto; cursor:pointer; float:left;';
        var removeStyles = 'margin:10px 0 10px 10px; padding:1px 5px; text-align:center; width:auto; cursor:pointer; float:left;';
 
        // Insert the buttons
        $( 'table.ls-answers', thisQuestion).after('<div class="button-wrapper">\
            <button type="button" class="'+addClasses+'" style="'+addStyles+'">'+addContent+'</button>\
            <button type="button" class="'+removeClasses+'" style="display: none; '+removeStyles+'">'+removeContent+'</button>\
          </div>');
 
        // Listeners on the buttons
        $('.inserted-button.add', thisQuestion).on('click', function (event) {
          addRow();
        });
        $('.inserted-button.remove', thisQuestion).on('click', function (event) {
          removeRow();
        });
 
        // Define the relevant rows
        var relevantRows = $('tr.subquestion-list:not(.ls-irrelevant)', thisQuestion);
 
        // Function to add a row, show the "Remove" element and hide the "Add" element if all rows are shown
        function addRow() {
          $('[data-visible="false"]:first', thisQuestion).attr('data-visible', 'true').show();
          $('.inserted-button.remove', thisQuestion).show();
          console.log($('[data-visible="true"]', thisQuestion).length+' == '+$(relevantRows).length - 1);
          if ($('[data-visible="false"]', thisQuestion).length == 0)  {
            $('.inserted-button.add', thisQuestion).hide();
          }
          $('.inserted-button.add', thisQuestion).blur();
        }
 
        // Function to remove a row, clear the contents of the removed row,
        // show the "Add" element if the last row is hidden and hide the "Remove" element if only the first row is shown
        function removeRow() {
          $('[data-visible="true"]:last input:text', thisQuestion).val('').trigger('keyup');
          $('[data-visible="true"]:last .inserted-select', thisQuestion).val('').trigger('change');
          $('[data-visible="true"]:last', thisQuestion).attr('data-visible', 'false').hide();
          $('.inserted-button.add', thisQuestion).show();
          if ($('[data-visible="true"]', thisQuestion).length == 0)  {
            $('.inserted-button.remove', thisQuestion).hide();
          }
          $('.inserted-button.remove', thisQuestion).blur();
        }
 
        // Initially hide all except first row or any rows with populated inputs
        $(relevantRows).slice(1).each(function(i) {
          $( this ).attr('data-visible', 'false').hide();
 
          $('input[type=text]', this).each(function(i) {
            if ($.trim($(this).val()) != '') {
              $(this).closest('tr').attr('data-visible', 'true').show();
              $('.inserted-button.remove', thisQuestion).show();
            }
          });
        });
      }
    }
    // Call the function with a question ID
    varLengthArray({QID});
  });
</script>

Sample survey attached:  

File Attachment:

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

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 3 months ago #245029 by tpartner

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose