Welcome to the LimeSurvey Community Forum

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

Using previous Array question responses to set following Array subquestions

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 8 months ago #62842 by tpartner
Okay, now I'm confused about the total scope of the workaround. I don't see my latest code in that script.

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 8 months ago #62853 by MaLu
Oh, ok. Well, what I'm trying to do is to limit the size of some survey questions in rows and columns. I attached a screenshot what it should look like.

Following your previous script, the two multiple-option questions (x and x2) are both hidden (only for the test unhidden) and automatically filled by answering the array questions before them (Q1 respective Q4). For subsequent questions, I then used the filter option inserting x respective x2. So far the survey works perfectly.

In Q5, I’d like to limit both rows and columns. Hiding rows seems fairly easy using the filter again. Inserting your script to limit the columns, it only works, if I actively check or uncheck any box in x. Is it possible to transfer the automatically filled answers to Q5 as well?
I also attached the survey.

Well knowingly that I’m asking quite a lot, I hope you can help me out…

Best,
Marie
The topic has been locked.
More
12 years 8 months ago #63148 by MaLu
Is there an option to simulate the click in the hidden question so it pretends to be actively clicked?

Appreciate all the help and comments I can get!
Marie
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 8 months ago #63196 by tpartner
Sorry Marie for the delay.

Remove all scripts in "x", "x2" and "Q5". Insert the following script in the source of "x". This script is an aggregation of the 3 that you were using with the countChecked() function modified to handle the hide/show columns part.
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() { 
 
    // Call the show/hide rows function
    // Params - Array ID, Hidden question ID, Column question
    countChecked(1256, 846, 852);
    countChecked(1289, 1295);
 
    // Call the initialisation of the hide/show columns function
    // Params - the IDs of the checkbox question and the array question
    handleCols(846, 852);
 
    function countChecked(q1ID, qHiddenID, qColumns) {
 
      // 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];
      }
 
      // A listener on the 1st column of Q1 radio buttons 
      $('#question'+q1ID+' td.answer_cell_00A1').click(function () { 
 
        // Uncheck the corresponding option in the hidden question
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', false);
 
        // Fire the conditions function to hide the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType);
        if(qColumns) {
          hideShow(qHiddenID, qColumns);
        }
      });
 
      // A listener on the 2nd and 3rd columns of Q1 radio buttons 
      $('#question'+q1ID+' td.answer_cell_00A3, #question'+q1ID+' td.answer_cell_00A5').click(function () { 
 
        // Check the corresponding option in the hidden question
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true);
 
        // Fire the conditions function to show the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType);
        if(qColumns) {
          hideShow(qHiddenID, qColumns);
        }
      });
    }
 
    // The initialisation of the hide/show columns function
    function handleCols(qCheckbox, qArray) {
 
      // Assign column-specific classes to answer cells of the "column" question
      $('#question'+qArray+' table.question thead th').each(function(i){
        $(this).addClass('ansCol'+i+'');
      });
      $('#question'+qArray+' table.question tbody').each(function(i){
        $('td', this).each(function(i){
          $(this).addClass('ansCol'+i+'');
        });
      });
 
      // Initially hide or show array columns depending on checkboxes
      hideShow(qCheckbox, qArray);
    }
 
    // A function to hide or show columns depending on checkboxes in a multi-opt question
    function hideShow(qCheckbox, qArray) {
 
      // Loop through all multi-opt checkboxes
      $('#question'+qCheckbox+' input.checkbox').each(function(i){
        if($(this).attr('checked') == true) {
          $('#question'+qArray+' .ansCol'+i+'').show();
        }
        else {
          $('#question'+qArray+' .ansCol'+i+'').hide();
          $('#question'+qArray+' .ansCol'+i+' input[type="checkbox"]').attr('checked', false);
        }
      });
    }
 
  });
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 8 months ago #63198 by tpartner
Oh, and check those IDs in rows 7, 8 and 12. I'm pretty sure those are the IDs from your survey but you may need to change them if you have exported/imported the survey.

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 8 months ago #63217 by MaLu
Brilliant! That works perfectly now, just the way I wanted it to!!

And here’s my last question for now: Is it possible to show the questions one by one without paralysing the mechanism?

Thank you so much for your patient support! Hope I wasn’t bothering you too much…

Cheers,
Marie
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 8 months ago #63225 by tpartner
If you mean place the questions on different pages, no. The script can only manipulate question on the same page.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: MaLu
The topic has been locked.
More
12 years 5 months ago #65993 by Maximilien
Hi,

I had pretty much the same problem that the initial question described. I used the script you provided Tony, and I really thank you for all your work.

I have a little issue though, and I don't know how to fix it.
It appears that the listener
Code:
$('#question'+q1ID+' td.answer_cell_002').click(function () {

is listening on the cell of the array... Not on the checkboxes. My problem is that it works fine if the user (as most users do) clicks on the cell, but it fails if he clicks on the checkbox itself!
Quite trick. It's not too difficult to listen on a cell (all cells are identified by the same class "answer_cell_00xxx") but it looks more difficult to listen on the checkboxes of a column (they are identified by ids and names that differs from one to the other).

Am I missing something ?
Any idea to get this working ? (I do have users that click right on the checkbox sadly)

Thanks in advance,
Maximilien
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago #65996 by DenisChenu
Maybe something like that:
Code:
$('#question'+q1ID+' td.answer_cell_002, #question'+q1ID+' td.answer_cell_002 input:checkbox').click(function () {

The jquery api : api.jquery.com/

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.
The following user(s) said Thank You: Maximilien
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago - 12 years 5 months ago #66000 by tpartner

...is listening on the cell of the array... Not on the checkboxes.

This workaround is for array questions with radio buttons. What question type are you using?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 12 years 5 months ago by tpartner.
The following user(s) said Thank You: Maximilien
The topic has been locked.
More
12 years 5 months ago #66052 by Maximilien
Thanks Shnoulle, it worked !
I set up two listeners : one on the cell and one on the checkbox (using your code).

About the radio button Tony, I couldn't do that, I need to have checkboxes. It's an Array (Numbers) with a checkbox layout.

Another thing if you have some time (though it's not really important) :
I have Q1 which is this Array of checkboxes and Q2 which is another array depending on the results of the second column of Q1.
So Q2 rows only show when the equivalent row in Q1 column 2 is selected.
I used the following javascript code to get it to work using an hidden Multiple Options question.
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
 
    // Call the function
    // Params - Q1 ID, Q2 ID, Hidden question ID
    countChecked(529, 664, 4059);
 
    function countChecked(q1ID, q2ID, qHiddenID) {
 
      // 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];
      }
 
      // A listener on the 2nd of Q1 radio buttons 
      $('#question'+q1ID+' td.answer_cell_002').click(function () { 
        // Check the corresponding option in the hidden question
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true);
 
        // Fire the conditions function to show the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType)
      });
 
      $('#question'+q1ID+' td.answer_cell_002, #question'+q1ID+' td.answer_cell_002 input:checkbox').click(function () { 
        // Check the corresponding option in the hidden question
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true);
 
        // Fire the conditions function to show the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType)
      });
 
    }
 
  });
 
</script>

It works fine when the user checks a box in Q1 column2 : it displays the line in Q2. But
I would have liked to have some kind of toggle : if the user uncheck a box in column 2 of Q1 it should hide the correspondant row in Q2.
I tried this code that fails (it doesn't do anything - display or hide - but I can't understand why !) :
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
 
    // Call the function
    // Params - Q1 ID, Q2 ID, Hidden question ID
    countChecked(529, 664, 4059);
 
    function countChecked(q1ID, q2ID, qHiddenID) {
 
      // 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];
      }
 
      // A listener on the 2nd of Q1 radio buttons 
      $('#question'+q1ID+' td.answer_cell_002').click(function () { 
        // Check the corresponding option in the hidden question
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        if ($('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked') == true) {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', false)
        } else {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true)
        } 
        // Fire the conditions function to show the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType)
      });
 
      $('#question'+q1ID+' td.answer_cell_002, #question'+q1ID+' td.answer_cell_002 input:checkbox').click(function () { 
        // Check the corresponding option in the hidden question
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        if ($('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked') == true) {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', false)
        } else {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true)
        }
 
        // Fire the conditions function to show the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType)
      });
 
    }
 
  });
 
</script>

If you have any idea why it fails... ;-)
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 5 months ago - 12 years 5 months ago #66090 by tpartner
A couple of things:

1) You don't need the first listener - selectors for the cell and input elements are both included in the second listener.

2) There is a logic problem with the IF statement.

This is untested but try:
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
 
    // Call the function
    // Params - Q1 ID, Q2 ID, Hidden question ID
    countChecked(529, 664, 4059);
 
    function countChecked(q1ID, q2ID, qHiddenID) {
 
      // 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];
      }
 
      $('#question'+q1ID+' td.answer_cell_002, #question'+q1ID+' td.answer_cell_002 input:checkbox').click(function () { 
        // Check the corresponding option in the hidden question
        var parentRow = $(this).parents('tbody:eq(0)');
        var rowID = $(this).parents('tbody:eq(0)').attr('id');
        var tmp2 = rowID.split('X'+q1ID);
        var answerCode = tmp2[1];
        if ($('td.answer_cell_002 input:checkbox', parentRow).attr('checked') == true) {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', true)
        } else {
          $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('checked', false)
        }
 
        // Fire the conditions function to show the corresponding row in Q2
        var hiddenInputValue = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('value');
        var hiddenInputName = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('name');
        var hiddenInputType = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode).attr('type');
        checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType)
      });
 
    }
 
  });
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 12 years 5 months ago by tpartner.
The following user(s) said Thank You: Maximilien
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose