Welcome to the LimeSurvey Community Forum

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

Last Option In Array (Numbers) (Checkboxes) Row Excludes All Others

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #120483 by tpartner
What version of LimeSurvey are you using? Please give a link to a small test survey.

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 10 months ago - 8 years 10 months ago #120485 by AndrejL
Of course.

We are using 2.00+,. And can not upgrade at this point.

I exported just this question here zadovoljstvo.sparslovenija.si/limesurvey...x.php/784886/lang-sl

First example uses first script and second one uses modified script.
Last edit: 8 years 10 months ago by AndrejL.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #120486 by tpartner
2.00+ uses an old version of jQuery so you'll need to use 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 last 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('normalOpt');
    $('table.question tbody tr', thisQuestion).each(function(i) {
      // Last two coluns are exclusive
      $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt');
      $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt');
    });
 
    // A listener on the checkboxes
    $('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) {
      handleCheckboxes($(this).closest('td'))
    });
 
    // A listener on the checkbox cells
    $('table.question td.question-item', thisQuestion).click(function (event) {
      handleCheckboxes($(this));
    });
 
    function handleCheckboxes(thisCell) {
      // Set some vars
      var thisRow = thisCell.closest('tr');
      var thisInput = $('input[type=checkbox]', thisCell);
 
      if(thisInput.is(':checked')) {
 
        // Uncheck the appropriate boxes in the row
        if ($(thisCell).hasClass('normalOpt')) { // Non-exclusive
          $('.exlusiveOpt input[type=checkbox]', thisRow).each(function(i) {
            $(this).attr('checked', false);
            $(this).closest('td').find('input[type="hidden"]').attr('value', '');
          });
        }
        else { // Exclusive
          $('input[type=checkbox]', thisRow).not(thisInput).each(function(i) {
            $(this).attr('checked', false);
            $(this).closest('td').find('input[type="hidden"]').attr('value', '');
          });
        }
      }
    }
  }
</script>

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 10 months ago #120492 by AndrejL
I works perfect!

Thank you Tony!
The topic has been locked.
More
5 years 11 months ago - 5 years 11 months ago #167867 by krosser

tpartner wrote: Try this to make the last two columns exclusive:

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 last 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('normalOpt');
    $('table.question tbody tr', thisQuestion).each(function(i) {
      // Last two coluns are exclusive
      $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt');
      $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt');
    });
 
    // A listener on the checkboxes
    $('table.question tbody td input[type=checkbox]', thisQuestion).change(function (event) {
 
      if($(this).is(':checked')) {
        // Set some vars
        var thisRow = $(this).closest('tr');
        var thisCell = $(this).closest('td');
 
        // Uncheck the appropriate boxes in the row
        if ($(thisCell).hasClass('normalOpt')) { // Non-exclusive
          $('.exlusiveOpt input[type=checkbox]', thisRow).each(function(i) {
            $(this).prop('checked', false);
            $(this).closest('td').find('input[type="hidden"]').attr('value', '');
          });
        }
        else { // Exclusive
          $('input[type=checkbox]', thisRow).not(this).each(function(i) {
            $(this).prop('checked', false);
            $(this).closest('td').find('input[type="hidden"]').attr('value', '');
          });
        }
      }
    });
  }
</script>



I also have this issue in multiple choice when I need to make two last columns to deselect/cancel answers in the other columns. Unfortunately, this code does not work in LS 3.6-3.7.
Does someone have a working solution for it?

The example question attached to the post.

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
Last edit: 5 years 11 months ago by krosser. Reason: LSQ files are not allowed to attach... Had to zip it.
The topic has been locked.
More
5 years 11 months ago #167886 by krosser
Actually, I've made a test survey instead of that single question in LSQ.
:)

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 10 months ago - 5 years 10 months ago #168089 by tpartner
To render specific array columns as exclusive in 3.x...

1) Place this in your theme custom.js file:

Code:
// A jQuery plugin to render column(s) in checkbox arrays exclusive
(function( $ ){
 
  $.fn.cbExclusiveColumns = function(options) {  
 
    var opts = $.extend( {
      //columns: [3,4] // Column(s) to be exclusive (comma-separated) 
    }, options);
 
    return this.each(function() { 
 
      var thisQuestion = $(this)
 
      // Add some classes to the checkbox cells
      $('td.answer-item', thisQuestion).addClass('normal-item');
      $('tr.subquestion-list', thisQuestion).each(function(i) {
        var thisRow = $(this);
        $.each(opts.columns, function(i, val) {
          $('td.answer-item:eq('+(val-1)+')', thisRow).removeClass('normal-item').addClass('exclusive-item');
        });
      });
 
      // A listener on the checkboxes
      $('input[type="checkbox"]', thisQuestion).on('change', function (event) {
        handleExclusive($(this).closest('td'));
      });
 
      function handleExclusive(thisCell) {
 
        var thisRow = $(thisCell).closest('tr');
 
        // Uncheck the appropriate boxes in a row
        if ($(thisCell).hasClass('normal-item')) {
          $('.exclusive-item input[type="checkbox"]', thisRow).prop('checked', false);
          $('.exclusive-item input[type="text"]', thisRow).val('');
        }
        else {
          $('.answer-item', thisRow).not(thisCell).find('input[type="checkbox"]').prop('checked', false);
          $('.answer-item', thisRow).not(thisCell).find('input[type="text"]').val('');
        }
 
        // Check conditions (relevance)
        $('td.answer-item', thisRow).each(function(i) {
          var thisValue = '';
          if($('input[type="checkbox"]', this).is(':checked')) {
            thisValue = 1;
          }
          var thisSGQA = $('input[type="checkbox"]', this).attr('id').replace(/cbox_/, '');
 
          $('input[type="hidden"]', this).attr('value', thisValue);
          fixnum_checkconditions(thisValue, thisSGQA, 'hidden');
        });
      }
    });
 
  };
})( jQuery );


2) Place something like this in the source of the question (in this case, columns 3 and 4 will be exclusive):

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    $('#question{QID}').cbExclusiveColumns({
      columns: [3,4] // Column(s) to be exclusive (comma-separated)
    });
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 10 months ago by tpartner.
The following user(s) said Thank You: krosser
The topic has been locked.
More
5 years 10 months ago #168101 by krosser
Have just tested it and it works nicely! Many thanks for your help, Tony!!

I'm using the latest LS 3.22 hosted on LS servers, not installed locally.
The topic has been locked.
More
4 years 10 months ago #184534 by KaryG
Hi,
I would like to alter the script from this post to make exclusive last 3 elements in each array row. Because I'm no javascript expert I don't know how to do it myself. Can you help me?
Thanks!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 10 months ago #184539 by Joffm
You saw this in tpartner's last post

<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
$('#question{QID}').cbExclusiveColumns({
columns: [3,4] // Column(s) to be exclusive (comma-separated)
});
});
</script>

Here you enter your "exclusive" columns.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: KaryG
The topic has been locked.
More
4 years 10 months ago #184545 by KaryG
Thanks for the help! I will try in this way.
The topic has been locked.
More
4 years 9 months ago #185803 by davebostockgmail
Following on from this is there any way to make the last row of an array numbers with checkboxes exclusive

For example

A B
Option 1 [ ] [ ]
Option 2 [ ] [ ]
Exclusive [ ] [ ]

So in this case you can select both option 1 and 2 as a multiple but the exclusive option behaves as it would in a multiple choice question?

Version 3.17.5

Thanks

Dave
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose