Script: Checkbox with Mutually Exclusive Groups

This article will help you setup a checkbox question to have mutually exclusive groups (or prohibited pairs)


Background

You may have scenarios in which you have a 'Checkbox' question with mutually exclusive groups or prohibited pairs. This script will help you set that up. 


Instructions

  1. Program your 'Checkbox' question
  2.  Add the following code to the 'ADD JAVASCRIPT' field
    • See article: Adding JavaScript and/or CSS to a Question
    • Update the 'array1' and 'array2' fields with the comma delimited list of your mutually exclusive items. In the code below, if options 1, 2 and/or 3 (array1) are selected and then options 4, 5 and/or 6 (array2) are selected, then the opposite array will be unchecked. 
      var $ = jQuery,
          group1 = [1, 2, 3],
          group2 = [4, 5, 6];
      
      
      $("input[type='checkbox']").change(function(){
        var checked = $(this).prop("checked");
        if (checked) {
          var curName = +$(this).attr("data-name");
          var otherGuys = [];
          if ( group1.indexOf(curName) > -1 ) {
            otherGuys = group2;
          }
          else if ( group2.indexOf(curName) > -1 ) {
            otherGuys = group1;
          }
          $.each(otherGuys, function(i, v){
            $("input[data-name='" + v + "']").prop("checked", false);
          });
        }
      });
      		

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us