Welcome to the LimeSurvey Community Forum

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

Exclusive last row in multiple choice (after piping and conditional questions)

  • Jenncr93
  • Jenncr93's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204444 by Jenncr93
Hi there. I have browsed and tried many options from other forum posts. For some reason the Java script I am using in one of my multiple choice questions is not working to uncheck all the other multiple choice options if the last option is checked (i.e. Don't know). But it works for other questions where I have used it. I am thinking that the problem is that that question depends heavily on other questions (piping and conditionals). I am not a JavaScript savvy person nor a LimeSurvey expert, everything I did on my survey I got it from other forum posts (so I basically copy-paste any Javascript code, but do not understand it. However, I now understand more the code used in LS, and can do it on my own). I am attaching two screenshots, one is the question that would lead you to the question that is not working. I am also attaching the question structure so you can see my code.

I am using this javascript:

<script type='text/javascript'>

// Set check or unchecked all checkboxes
function checkAll(e) {
var checkboxes = document.getElementsByName('check');

if (e.checked) {
for (var i = 0; i < checkboxes.length; i++) {
checkboxes.checked = true;
}
} else {
for (var i = 0; i < checkboxes.length; i++) {
checkboxes.checked = false;
}
}
}

// Hide Checked rows
function hideChecked(){
var checkboxes = document.getElementsByName('check');

for (var i = 0; i < checkboxes.length; i++) {
var checkid = checkboxes.id;
var split_id = checkid.split("_");
var rowno = split_id[1];
if(checkboxes.checked){
document.getElementById("tr_"+rowno).style.display="none";
}
}

}

// Reset layout
function reset(){
var checkboxes = document.getElementsByName('check');
document.getElementsByName("showhide")[0].checked=false;

for (var i = 0; i < checkboxes.length; i++) {
var checkid = checkboxes.id;
var split_id = checkid.split("_");
var rowno = split_id[1];
document.getElementById("tr_"+rowno).style.display="table-row";
checkboxes.checked = false;
}

}
</script>

Please let me know if you need anything else. I don't think I would be able to upload the whole survey though :/ Thank you so much for any help you can give me :)!!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago - 3 years 7 months ago #204449 by tpartner

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 7 months ago by tpartner.
The topic has been locked.
  • Jenncr93
  • Jenncr93's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204452 by Jenncr93
Thank you so much Tony!! I had completely overlooked this option. However, participants can still check other options and also check "don't know" (see image attached). This does not open up any further questions that depend on them but it would still be nice for those other other options to be unchecked like the javascript did for the other questions. Do you have any thoughts on why the script would not be working for this question? Do you recommend using another script?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago #204472 by tpartner
I'm not sure how that script would have ever worked.

This script will render a multiple-choice sub-question exclusive, unchecking all other sub-questions when checked. In this example, we set sq code "SQ005" as the exclusive item.

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Define the exclusive sub-question code
    var exclusiveCode = 'SQ005';
 
    // Identify this question
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
 
    // Assign classes
    $('.answer-item[id$="X'+qID+exclusiveCode+'"]', thisQuestion).addClass('exclusive-item');
 
    // Listener on the checkboxes
    $('input:checkbox', thisQuestion).on('change', function(e) {
      var thisItem = $(this).closest('.answer-item');
      if(thisItem.hasClass('exclusive-item') &amp;&amp; $(this).is(':checked')) {
        $('.answer-item:not(.exclusive-item) input:hidden', thisQuestion).val('');
        $('.answer-item:not(.exclusive-item) input:checkbox', thisQuestion).prop('checked', false).trigger('change');
      }
      else if($(this).is(':checked')) {
        $('.answer-item.exclusive-item input:hidden', thisQuestion).val('');
        $('.answer-item.exclusive-item input:checkbox', thisQuestion).prop('checked', false).trigger('change');
      }
    });
 
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...5(1).lss
File Size:19 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.
  • Jenncr93
  • Jenncr93's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #204492 by Jenncr93
Thank you so much!! That worked like a charm!!
I don't know either! but it still works on the other questions!!
Thank you again!!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose