Welcome to the LimeSurvey Community Forum

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

Unchecking excluded checkboxes

More
8 years 11 months ago #141400 by BBCMResearch
Hello,

I have several multiple choice questions in a survey that use the exclusive option for a "none of the above" answer.

If someone clicks other answer options before clicking "none of the above," these other selected answers become grayed-out, which normally would be fine. However, I need them to become "un-checked" as well.

Based on the solution tpartner gave here , it appears that I can insert a script to uncheck other boxes if the exclusive option is selected.

However, I don't know enough to code it. I would greatly appreciate it if someone could help me out.

I use Version 2.50+160901

Thanks!
The topic has been locked.
More
8 years 11 months ago #141447 by tpartner
Assuming the exclusive item is last, add this to the question source of the multiple choice questions:

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', '');
        });
      }
    });
  });
</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: BBCMResearch
The topic has been locked.
More
8 years 11 months ago #141463 by BBCMResearch
Excellent as always.

However, I do have one question where the last two items in the list are both exclusive. Could you adapt the script to work for both items?

Thanks!
The topic has been locked.
More
8 years 11 months ago #141469 by tpartner
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Uncheck all excluded items
    var itemsLength = $('div.question-item', thisQuestion).length;
    $('div.question-item:gt('+(itemsLength-3)+') 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', '');
        });
      }
    });
  });
</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: BBCMResearch
The topic has been locked.
More
8 years 11 months ago #141471 by BBCMResearch
Thanks!
The topic has been locked.
More
8 years 8 months ago #146209 by BBCMResearch
Tony,

Thanks for your previous help with this. I now need to use this same script, but with an Other option enabled. However, the Other option is not the exclusive one. So you can enter text into the other field, and that remains visible after the exclusive option is checked.

Is it possible to reset the text field for the other option when the exclusive option is checked?

Thanks!
The topic has been locked.
More
8 years 8 months ago #146232 by DenisChenu

BBCMResearch wrote: .....
Is it possible to reset the text field for the other option when the exclusive option is checked?
....

Don't crosspost please ......
www.limesurvey.org/forum/can-i-do-this-w...lusive-option#146231

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
The topic has been locked.
More
8 years 8 months ago #146252 by tpartner
BBCMResearch, is the exclusive item last?

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
8 years 8 months ago #146271 by BBCMResearch
Hi Tony,

Yes, the exclusive item is last, and the other option is just before it.
The topic has been locked.
More
8 years 8 months ago #146285 by tpartner
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>

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: BBCMResearch
The topic has been locked.
More
8 years 8 months ago #146286 by BBCMResearch
Works like a charm, thanks Tony!
The topic has been locked.
More
8 years 8 months ago - 8 years 8 months ago #146292 by DenisChenu
Hi Tony,

Just something about javascripting answer and EM, think it's better to use
$(this).prop('checked', false).trigger('change');
$('input[type="text"]', thisQuestion).val('').triiger('keyup')

Unsure it work on 2.5X, but i hope it work directly in 3.0 : see github.com/LimeSurvey/LimeSurvey/blob/de...em_javascript.js#L22

If it don't work : it's a bug

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
Last edit: 8 years 8 months ago by DenisChenu.
The topic has been locked.
More
8 years 8 months ago #146301 by tpartner
Hmm...I just tested in 2.5.7 and don't seem to need those trigger events to fire Expression Manager

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
8 years 8 months ago #146306 by DenisChenu

tpartner wrote: Hmm...I just tested in 2.5.7 and don't seem to need those trigger events to fire Expression Manager

Yes, because it was inlined
Code:
<input type="radio" onclick="checkconditions($(this).val(), $(this).attr('name'), 'radio', 'click') />
And, sometimes this part was lost in HTML. Using a global event don't add an event to each input , and workaround can be more easy.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
The topic has been locked.
More
8 years 7 months ago #146649 by andre_twang
Hi Tony,

Your scripts have been extremely helpful! Could I ask what about unchecking excluded checkboxes for "Multiple Choice with Comments" question type, please? This script seems to work for "Multiple Choices" but not for the "Multiple Choices with Comments"

Warm regards,

Andre
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose