Welcome to the LimeSurvey Community Forum

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

fill ranking array with answers from previous question

  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 3 months ago #70794 by tpartner
Are Q1 and Q2 on the same page?

Are you using group-by-group mode so you can insert a hidden 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.
More
12 years 3 months ago #70796 by hmoore1024
I have them on different pages, assuming that the answers for Q1 would need to "submit" before being able to be read for Q2.

They're presented group by group and read through the instructions for hidden questions - I just wasn't sure how that would work for an array where I'm looking to pull options with specific responses forward.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 3 months ago - 12 years 3 months ago #70815 by tpartner
Okay, in that case you can add a hidden multiple-options question (qHidden) to control the display of Q2 with an array filter . A script can auto-check the corresponding option in qHidden if an answer in columns 1 or 2 of Q1 are clicked.

1) Set up your survey to use JavaScript .

2) Add a multiple-options question (qHidden) to the group containing Q1. Give it the same sub-questions and codes as Q1.

3) Add an array filter to Q2 based on qHidden.

4) Add the following script to the source of Q1. Replace "11" with the Q1 question ID and "HH" with the qHidden question ID .

The script:
- Hides the multiple-options question (qHidden)
- Puts a listener on the array cells of Q1.
- If a cell in column 1 or 2 of Q1 is clicked, the corresponding option in qHidden is checked.
- If a cell in any other column of Q1 is clicked, the corresponding option in qHidden is unchecked.
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() { 
 
    // Call the autoClick function
    // Params - Array ID, Hidden question ID, Column of array that auto-checks the hidden question
    autoClick(11, HH, 1);
    autoClick(11, HH, 2);
 
    function autoClick(qArrayID, qHiddenID, column) {
 
      // Hide the hidden question
      $('#question'+qHiddenID+'').hide();
 
      // Find the survey and group IDs
      if($('input#fieldnames').length != 0) {
        var fieldNames = $('input#fieldnames').attr('value');
        var tmp = fieldNames.split('X');
        var sID = tmp[0];
        var gID = tmp[1];
      } 
 
      // Add some column-specific classes
      $('#question'+qArrayID+' table.question tr').each(function(i, el){
        $('> *', this).each(function(i, el){
          $(el).addClass('col-'+i);
        });
      });
 
      // Add a class to answers that are to auto-check the hidden question
      $('#question'+qArrayID+' .col-'+column).addClass('autoClick');
 
      // A listener on the Q1 radio buttons 
      $('#question'+qArrayID+' table.question tbody td').click(function () {
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+qArrayID);
        var answerCode = tmp2[1];
        // This cell checks the corresponding option it qHidden
        if($(this).hasClass('autoClick')) {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true);
        }
        // This cell unchecks the corresponding option it qHidden
        else {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', false);
        }
      });
    }
 
  });
 
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 12 years 3 months ago by tpartner.
The topic has been locked.
More
11 years 5 months ago #86586 by rvas
Hi all! I am using LimeSuvey 2+ and I have a problem trying to do something similar.

I want to do the same as described earlier but in the same page. I managed to do it properly in two different pages using the code provided above with a slight modification
(I replaced the line 'var rowID = $(this).parents('tbody:eq(0)').attr('id');' with 'var rowID = $(this).parents('tr:eq(0)').attr('id');')

Unfortunately while I have added an array filter to the Q2 array based on qHidden, it cannot capture the checkboxes of qHidden if they are triggered by javascript. Is there a workaround to that?

Many thanks in advance!
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
11 years 2 months ago #90542 by Mazi
Please attach your survey for testing so we can check the details of the implementation.

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose