Welcome to the LimeSurvey Community Forum

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

Unchecking excluded checkboxes

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 3 months ago #146681 by tpartner
Replied by tpartner on topic Unchecking excluded checkboxes
Here is an updated version if the script that should handle both types of multiple-choice.

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Add some classes
    $('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item');
    $('.question-item:last', thisQuestion).addClass('exclusive-item');
 
    // Handle exclusive items
    $('input.checkbox', thisQuestion).on('change', function(e) {
      if($(this).is(':checked')) {
        var actionItems = $('.non-exclusive-item', thisQuestion);
        if($(this).closest('.question-item').hasClass('non-exclusive-item')) {
          actionItems = $('.exclusive-item', thisQuestion);
        }
        actionItems.each(function(i) {
          $('input.checkbox', this).prop('checked', false).trigger('change');
          $('input:hidden', this).attr('value', '');
          $('input[type="text"]', this).val('').trigger('keyup');
        });
      }
    });
 
  });
</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: andre_twang
The topic has been locked.
More
7 years 3 months ago #146705 by andre_twang
Replied by andre_twang on topic Unchecking excluded checkboxes
Thanks Tony! It worked really well!
The topic has been locked.
More
6 years 6 hours ago #167341 by tixeon
Replied by tixeon on topic Unchecking excluded checkboxes
I have tried using this script in version 2.6.7-lts Build 171212 and it doesn’t work. Is there an updated code that will?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 hours ago #167343 by Joffm
Replied by Joffm on topic Unchecking excluded checkboxes
Hi, tixeon,

In the first post of this thread you see that it is about the 2.50-2.73 branch.

You work with the older 2.06 branch, which was renamed - to confuse some users - to 2.6.x

So you cannot expect the script to work.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago #167427 by tpartner
Replied by tpartner on topic Unchecking excluded checkboxes

I have tried using this script in version 2.6.7-lts Build 171212 and it doesn’t work.

I just tested and it does work in 2.06 LTS. Can you attach a small sample survey containing only the affected 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
5 years 11 months ago #167924 by krosser
Replied by krosser on topic Unchecking excluded checkboxes

tpartner wrote: Try this:

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Uncheck all excluded items
    $('div.question-item:last input.checkbox', thisQuestion).on('change', function(e) {
      if($(this).is(':checked')) {
        $('input.checkbox', thisQuestion).not($(this)).each(function(i) {
          $(this).prop('checked', false);
          $(this).nextAll('input:hidden:eq(0)').attr('value', '');
        });
        $('input[type="text"]', thisQuestion).val('');
      }
    });
 
  });
</script>


Just tested these workarounds and, unfortunately, they do not work anymore in LS 3.7. :(

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago - 5 years 11 months ago #167932 by tpartner
Replied by tpartner on topic Unchecking excluded checkboxes
Try changing all instances of 'input.checkbox' to 'input [type="checkbox"]'

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 11 months ago by tpartner.
The topic has been locked.
More
5 years 11 months ago #167950 by krosser
Replied by krosser on topic Unchecking excluded checkboxes

tpartner wrote: Try changing all instances of 'input.checkbox' to 'input [type="checkbox"]'


Tried to change it in both lines where it is, but it didn't solve it.

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago #167964 by tpartner
Replied by tpartner on topic Unchecking excluded checkboxes
Sorry, I am away this week with only a phone so can't debug any further.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago #168095 by tpartner
Replied by tpartner on topic Unchecking excluded checkboxes
This will work in 3.x:

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Add some classes
    $('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item');
    $('.question-item:last', thisQuestion).addClass('exclusive-item');
 
    // Handle exclusive items
    $('input[type="checkbox"]', thisQuestion).on('change', function(e) {
      if($(this).is(':checked')) {
        var actionItems = $('.non-exclusive-item', thisQuestion);
        if($(this).closest('.question-item').hasClass('non-exclusive-item')) {
          actionItems = $('.exclusive-item', thisQuestion);
        }
        actionItems.each(function(i) {
          $('input[type="checkbox"]', this).prop('checked', false).trigger('change');
          $('input:hidden', this).attr('value', '');
          $('input[type="text"]', this).val('').trigger('keyup');
        });
      }
    });
    $('input[type="text"]', thisQuestion).on('keyup change', function(e) {
      var thisInput = $(this);
      setTimeout(function() {
        $(thisInput).closest('.question-item').find('input[type="checkbox"]').trigger('change');
      }, 200);
    });
 
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...2965.lss
File Size:16 KB

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
5 years 9 months ago #171282 by Andrea01
Replied by Andrea01 on topic Unchecking excluded checkboxes
Hi Tony,

for your script:

<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

// Identify this question
var thisQuestion = $('#question{QID}');

// Add some classes
$('.question-item:not(:last)', thisQuestion).addClass('non-exclusive-item');
$('.question-item:last', thisQuestion).addClass('exclusive-item');

// Handle exclusive items
$('input.checkbox', thisQuestion).on('change', function(e) {
if($(this).is(':checked')) {
var actionItems = $('.non-exclusive-item', thisQuestion);
if($(this).closest('.question-item').hasClass('non-exclusive-item')) {
actionItems = $('.exclusive-item', thisQuestion);
}
actionItems.each(function(i) {
$('input.checkbox', this).prop('checked', false).trigger('change');
$('input:hidden', this).attr('value', '');
$('input[type="text"]', this).val('').trigger('keyup');
});
}
});

});
</script>

Is there a way to handle two exclusive options???

Thanks and best regards
Andrea
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 8 months ago #171315 by tpartner
Replied by tpartner on topic Unchecking excluded checkboxes
This script will render the last two items exclusive:

Code:
<script type="text/javascript" charset="utf-8">    
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Add some classes
    var sqCount = $('.question-item', thisQuestion).length;
    $('.question-item', thisQuestion).addClass('non-exclusive-item');
    $('.question-item:eq('+(sqCount-2)+')', thisQuestion).removeClass('non-exclusive-item').addClass('exclusive-item'); // 2nd last item
    $('.question-item:eq('+(sqCount-1)+')', thisQuestion).removeClass('non-exclusive-item').addClass('exclusive-item'); // Last item
 
    // Handle exclusive items
    $('input[type="checkbox"]', thisQuestion).on('change', function(e) {
      if($(this).is(':checked')) {
        var thisItem = $(this).closest('.question-item');
        var actionItems = $('.question-item', thisQuestion).not($(thisItem));
        if($(thisItem).hasClass('non-exclusive-item')) {
          actionItems = $('.exclusive-item', thisQuestion);
        }
        actionItems.each(function(i) {
          $('input[type="checkbox"]', this).prop('checked', false).trigger('change');
          $('input:hidden', this).attr('value', '');
          $('input[type="text"]', this).val('').trigger('keyup');
        });
      }
    });
    $('input[type="text"]', thisQuestion).on('keyup change', function(e) {
      var thisInput = $(this);
      setTimeout(function() {
        $(thisInput).closest('.question-item').find('input[type="checkbox"]').trigger('change');
      }, 200);
    });
 
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...9651.lss
File Size:17 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose