Welcome to the LimeSurvey Community Forum

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

Disable specific columns checkboxes if condition in array (numbers)

  • bellagambad
  • bellagambad's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 months 1 week ago #254735 by bellagambad
Your LimeSurvey version: 5.4.3+220926
Own server or LimeSurvey hosting: own server
Survey theme/template: fruity custom
==================
Hello LS forum !
In an array (numbers) question with 4 columns, I'd like to ensure (on each row) that when the checkbox in the second column is not activated, it's impossible to activate the checkboxes in columns 3 and 4. The checkbox in column 1 remains usable, regardless of the state of the checkbox in column 2. I found a topic pretty close to my request but couldn't manage to adapt the code provided by tpartner  to make it work.
In the sample I provided attached, activating the checkbox "option 2" should enable "option 3" and "option 4" (on each row independently). Otherwise, they must remain disabled. 
Thank you in advance for your help,
David
 

Please Log in to join the conversation.

  • bellagambad
  • bellagambad's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 months 4 days ago #254901 by bellagambad
I'd like to bring up this topic again, as I still haven't found the solution. Does anyone have any ideas?

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 4 days ago #254915 by Joffm
Hi,
it is not necessary to push after less than 5 days with a weekend in between.
Remember, we all are volunteers who try to help in our spare time.

First make sure you use a suitable script.
Tony's answer in your linked thread refers to a question where is mentioned

I would like to implement this code on version 2.73.1


You may use this one (the second) as a starting point.
[url] forums.limesurvey.org/forum/can-i-do-thi...clusive-optio#230493 [/url]

You may add a third class ".neutral-item" to the first column and adapt the function "handleExclusive(thisInput)"
 
Joffm

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

Please Log in to join the conversation.

  • bellagambad
  • bellagambad's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 months 3 days ago - 3 months 3 days ago #254918 by bellagambad
Of course, and I'm sorry if you saw my message as a lack of respect or consideration for your work. On the contrary, your help (and that of tpartner and DenisChenu) is greatly appreciated. From my past experiences on the forum (which is small), either the answer arrives quickly, or it doesn't. So I preferred to post again because of the short deadline imposed on me at work. Please accept my apologies.
So thank you very much for your reply and I'll see if I can find the solution with these indications using chatgpt. I have virtually no skills with this language so the latter could help.
Thanks again!
David

EDIT : with a couple of back and forth with ChatGPT and using tpartner's code and your input, I ended up with the following code, that seems to do what I want
Code:
<script type="text/javascript" data-author="DBEL">
  $(document).ready(function() {
    // Call the exclude function using question ID
    excludeOpt({QID});
  });
 
  // A function to set up exclusivity
  function excludeOpt(qID) {
    var thisQuestion = $('#question' + qID);
 
    // Set up exclusivity for each row
    thisQuestion.find('tr.subquestion-list').each(function() {
      setupRowExclusivity($(this));
    });
 
    // A listener on the checkboxes
    thisQuestion.find('input[type="checkbox"]').on('change', function(event) {
      handleExclusive($(this));
    });
 
    function setupRowExclusivity(row) {
      row.find('td.checkbox-item').each(function(index) {
        if (index === 1) {
          $(this).addClass('exclusive-item');
        } else if (index === 0) {
          $(this).addClass('neutral-item');
        } else {
          $(this).addClass('normal-item');
        }
      });
 
      $('.exclusive-item input[type="hidden"]', row).val('');
 
      // Initially disable the normal items
      $('.normal-item input[type="checkbox"]', row).prop('disabled', true);
    }
 
    function handleExclusive(thisInput) {
      var thisCell = thisInput.closest('td');
      var thisRow = thisInput.closest('tr');
 
      if ($(thisCell).hasClass('neutral-item')) {
        // Do nothing or add specific logic for the neutral item
      } else if ($(thisCell).hasClass('exclusive-item')) {
        // If exclusive item is checked, enable/disable normal items accordingly
        var normalItems = $('.normal-item input[type="checkbox"]', thisRow);
        normalItems.prop('disabled', !thisInput.is(':checked'));
        if (!thisInput.is(':checked')) {
          normalItems.prop('checked', false);
        }
      }
 
      // Check conditions (relevance)
      $('td.checkbox-item', thisRow).each(function() {
        var thisValue = $('input[type="checkbox"]', this).is(':checked') ? 1 : '';
        var thisSGQA = $('input[type="checkbox"]', this).attr('id').replace(/cbox_/, '');
 
        $('input[type="hidden"]', this).attr('value', thisValue);
        fixnum_checkconditions(thisValue, thisSGQA, 'hidden');
      });
    }
  }
</script>
Last edit: 3 months 3 days ago by bellagambad. Reason: solution maybe found

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 3 days ago - 3 months 3 days ago #254921 by tpartner
Here is more efficient version:
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
 
    $(document).on('ready pjax:scriptcomplete',function(){
 
        var thisQuestion = $('#question{QID}')
 
        // Add some classes to the checkbox cells
        // 3rd children (answer column 2)
        $('td.checkbox-item:nth-child(3)', thisQuestion).addClass('exclusive-item');        
        // 4th and above children (answer columns greater than 2)
        $('td.checkbox-item:nth-child(n+4)', thisQuestion).addClass('excluded-item');
 
        // A listener on the exclusive checkboxes
        $('.exclusive-item input:checkbox', thisQuestion).on('change', function (event) {
            handleExclusive($(this));
        });
 
        // Returning to page
        $('.exclusive-item input:checkbox', thisQuestion).each(function (i) {
            handleExclusive($(this));
        });
 
        function handleExclusive(thisInput) {
 
            var thisCell = $(thisInput).closest('td'); 
            var thisRow = $(thisCell).closest('tr');
 
            if(thisInput.is(':checked')) {
                // Enable the excluded checkboxes
                $('.excluded-item input:checkbox', thisRow).prop('disabled', false);
            }
            else {
                // Uncheck the excluded boxes in a row
                $('.excluded-item input[type="hidden"]', thisRow).val('');
                $('.excluded-item input:checkbox', thisRow).prop('checked', false).trigger('change').prop('disabled', true);
            }
        }
    });
</script>

Sample survey attached:  

File Attachment:

File Name: limesurvey...5841.lss
File Size:35 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 months 3 days ago by tpartner.

Please Log in to join the conversation.

  • bellagambad
  • bellagambad's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 months 3 days ago #254922 by bellagambad
It works perfectly, thank you very much. So if I understand correctly, as you specified the exclusive (3rd children) and this excluded (4th and above), you don't need to add a neutral class, is that right ?

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 3 days ago #254951 by tpartner
I don't see a need for a neutral class.

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.

Lime-years ahead

Online-surveys for every purse and purpose