Welcome to the LimeSurvey Community Forum

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

Conditions not working

  • w0928
  • w0928's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 5 months ago - 10 years 5 months ago #100167 by w0928
Conditions not working was created by w0928
I have an array(numbers) checkbox style question with 4 columns. The first 2 columns are mutually exclusive from all the others... so the user can only check column 1 by itself, column 2 by itself, column 3 with or without 4, and column 4 with or without 3.

I have the code below that implements this behavior.

In addition to this question I have another question with a condition on it that says to only show that question when the user has checked columns 2, 3, or 4 for one of the rows in my array(numbers) checkbox style question. I know the condition is set properly b/c if I remove the code below it works.

If a user checks a checkbox in columns 2, 3, or 4 then condition works and the question is shown. However, if the user checks on column 1, which triggers the code to uncheck columns 2, 3, and 4... the subsequent question still appears. It doesn't go away like it should.

So basically I am asking why the code below is causing this and how can I go about fixing it? I'm guessing I need to call .trigger('click') to uncheck those columns, but then I get stuck in a loop. Sorry if this is more of a javascript question. I'm just trying to figure out how this all works and would really, really appreciate any help you all could provide.

Thanks.
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
    // Call the exclude function using question ID
    excludeOpt ({QID});
  });
 
  // A function to make the first option in each array row exclusive
  function excludeOpt (qID) {
 
    var thisQuestion = $('#question'+qID)
 
    // Add some classes to the checkbox cells
    $('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt1');
    $('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt2');
    $('table.question tbody tr', thisQuestion).each(function(i) {
      $('.answer_cell_001:first', this).removeClass('nonExclusiveOpt1').addClass('exclusiveOpt1');
      $('.answer_cell_002:first', this).removeClass('nonExclusiveOpt2').addClass('exclusiveOpt2');
    });
 
    // A listener on the checkbox cells
    $('table.question tbody td', thisQuestion).click(function (event) {
 
      // Set some vars
      var thisRow = $(this).closest('tr');
 
      // Uncheck the appropriate boxes in a row
      if ($(this).hasClass('exclusiveOpt1')) {
        $('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
      }        
      else if ($(this).hasClass('exclusiveOpt2')) {
        $('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
      else if ($(this).hasClass('nonExclusiveOpt1') &amp;&amp; $(this).hasClass('nonExclusiveOpt2')) {
        $('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
        $('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
    });
 
    // A listener on the checkboxes
    $('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) {
 
      // Set some vars
      var thisRow = $(this).closest('tr');
      var thisCell = $(this).closest('td');
 
      // Uncheck the appropriate boxes in a row
      if ($(thisCell).hasClass('exclusiveOpt1')) {
        $('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
      }
      else if ($(thisCell).hasClass('exclusiveOpt2')) {
        $('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
      else if ($(thisCell).hasClass('nonExclusiveOpt1') &amp;&amp; $(thisCell).hasClass('nonExclusiveOpt2')) {
        $('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
        $('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
    });
  }  
</script>
Last edit: 10 years 5 months ago by w0928.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100190 by tpartner
Replied by tpartner on topic Conditions not working
Ah, we'll have to fire the fixnum_checkconditions() function for all affected checkboxes.

Try this:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
    // Call the exclude function using question ID
    excludeOpt ({QID});
  });
 
  // A function to make the first option in each array row exclusive
  function excludeOpt (qID) {
 
    var thisQuestion = $('#question'+qID)
 
    // Add some classes to the checkbox cells
    $('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt1');
    $('table.question tbody td', thisQuestion).addClass('nonExclusiveOpt2');
    $('table.question tbody tr', thisQuestion).each(function(i) {
      $('.answer_cell_001:first', this).removeClass('nonExclusiveOpt1').addClass('exclusiveOpt1');
      $('.answer_cell_002:first', this).removeClass('nonExclusiveOpt2').addClass('exclusiveOpt2');
    });
 
    // A listener on the checkbox cells
    $('table.question tbody td', thisQuestion).click(function (event) {
 
      // Set some vars
      var thisRow = $(this).closest('tr');
 
      // Uncheck the appropriate boxes in a row
      if ($(this).hasClass('exclusiveOpt1')) {
        $('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
      }        
      else if ($(this).hasClass('exclusiveOpt2')) {
        $('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
      else if ($(this).hasClass('nonExclusiveOpt1') &amp;&amp; $(this).hasClass('nonExclusiveOpt2')) {
        $('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
        $('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
 
      checkRowConditions(thisRow);
    });
 
    // A listener on the checkboxes
    $('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) {
 
      // Set some vars
      var thisRow = $(this).closest('tr');
      var thisCell = $(this).closest('td');
 
      // Uncheck the appropriate boxes in a row
      if ($(thisCell).hasClass('exclusiveOpt1')) {
        $('.nonExclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
      }
      else if ($(thisCell).hasClass('exclusiveOpt2')) {
        $('.nonExclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
      else if ($(thisCell).hasClass('nonExclusiveOpt1') &amp;&amp; $(thisCell).hasClass('nonExclusiveOpt2')) {
        $('.exclusiveOpt1 input[type=checkbox]', thisRow).attr('checked', false);
        $('.exclusiveOpt2 input[type=checkbox]', thisRow).attr('checked', false);
      }
 
      checkRowConditions(thisRow);
    });
 
    // A function to check conditions for all checkboxes in a row
    function checkRowConditions(thisRow) {
      $('input[type="checkbox"]', thisRow).each(function(i) {
        var thisAnsID = $(this).attr('id').replace(/cbox_/, '');
        var thisAnswerInput = document.getElementById('answer'+thisAnsID);
        var thisJavaInput = document.getElementById('java'+thisAnsID);
        var thisAnsValue = 0;
        if(this.checked) {
          thisAnsValue = 1; 
        } 
        thisAnswerInput.value = thisAnsValue;
        thisJavaInput.value = thisAnsValue;
        fixnum_checkconditions(thisAnsValue, thisAnsID, thisAnswerInput.type);  
        return true;
      });
    }
  }  
</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: w0928
The topic has been locked.
  • w0928
  • w0928's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 5 months ago #100192 by w0928
Replied by w0928 on topic Conditions not working
Thank you! I would have never figured this out on my own.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose