Welcome to the LimeSurvey Community Forum

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

Set a condition in a question - Loop?

  • kmera2021
  • kmera2021's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago #213740 by kmera2021
Set a condition in a question - Loop? was created by kmera2021
I need to create a condition for a registration form. A person can enter contact information for up to 10 participants. So if they select 3 participants, they should fill in 3 questions for each participant. Not sure if this is considered a loop or branching, but I hope is clear, since I am not sure how to do it. Any ideas?
Thanks and best regards,
 
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago #213748 by holch
Replied by holch on topic Set a condition in a question - Loop?
It is considered a loop, but Limesurvey doesn't have a loop function. But at least you have a finite number of possible "loops", so you can find a workaround.

Create a question group with all the questions regarding the contact information. Then copy it 9 times so that you have 10x the same question set.

Then hide/show those question groups via relevance equation based on the question with the number of respondents.

E.g. the second set would only show if the number of participants is 2 or more, then you would write something like number>1 into the relevance equation of the question group for the second participant. And so on.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The following user(s) said Thank You: kmera2021
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago #213770 by Joffm
Replied by Joffm on topic Set a condition in a question - Loop?
@holch gave you the basics how you "fake" a loop:

Depending on the number of contact information you'd like to collect, there might be a more appealing way.
Use an array(text) with 10 subquestions and the respective columns.
Then you can enter this javascript snippet toi insert the buttons and show/hide the rows accordingly
 
Code:
<script>
$(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 Contact';
            var removeContent = '[-] Remove Contact';
 
            // Create the Add and Remove elements &amp; 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>

And with a second snippet (javascript + css) you can define the column widths
 
Code:
<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: 8%; }
  .custom-array table.subquestion-list thead .column-1 {  width: 15%; }
  .custom-array table.subquestion-list thead .column-2 {  width: 40%; }
  .custom-array table.subquestion-list thead .column-3 {  width: 15%; }
  .custom-array table.subquestion-list thead .column-4 {  width: 22%; }
</style>
Adapt to your needs

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • kmera2021
  • kmera2021's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago #213813 by kmera2021
Replied by kmera2021 on topic Set a condition in a question - Loop?
That worked very well. Thank you for your support. 
 
The topic has been locked.
  • kmera2021
  • kmera2021's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago #213814 by kmera2021
Replied by kmera2021 on topic Set a condition in a question - Loop?
Thank you very much. My initial thought was to use a text array. However, when checking the responses exported to Excel, I saw that it was difficult to filter when people had some typos in their responses. So I found out that using the list dropdown allow me to get the correct answers such as gender or course selection.

I really like your option to add or remove a contact. Thanks again!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago #213819 by Joffm
Replied by Joffm on topic Set a condition in a question - Loop?

it was difficult to filter when people had some typos


Thderefore as often as possible use precoded lists.

And especially for this array(text) there is the option to insert drop-downs.

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