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
- Program your 'Checkbox' question
- See article: Add a Question: Checkbox (Multi-select)
- 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); }); } });