Sha256: 905b7747459aa1e6e5a46c63177d3603229ec4c850f11a0e3b89678233d3c228

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

// This component relies on JavaScript from GOV.UK Frontend
//= require govuk-frontend/components/checkboxes/checkboxes.js

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};
 (function (Modules) {
  'use strict'
   Modules.Checkboxes = function () {
    this.start = function (scope) {
      var _this = this;

      $(scope).find('[data-nested=true] input[type=checkbox]').on('change', function(e) {
        var checkbox = e.target;
        var isNested = $(checkbox).closest('.govuk-checkboxes--nested');
        var hasNested = $('.govuk-checkboxes--nested[data-parent=' + checkbox.id + ']');

        if (hasNested.length) {
          _this.toggleNestedCheckboxes(hasNested, checkbox);
        } else if (isNested.length) {
          _this.toggleParentCheckbox(isNested, checkbox);
        }
      });
    };

    this.toggleNestedCheckboxes = function(scope, checkbox) {
      if (checkbox.checked) {
        scope.find('input[type=checkbox]').prop("checked", true);
      } else {
        scope.find('input[type=checkbox]').prop("checked", false);
      }
    };

    this.toggleParentCheckbox = function(scope, checkbox) {
      var siblings = $(checkbox).parent('.govuk-checkboxes__item').siblings();
      var parent_id = scope.data('parent');

      if (checkbox.checked && siblings.length == siblings.find(':checked').length) {
        $('#' + parent_id).prop("checked", true);
      } else {
        $('#' + parent_id).prop("checked", false);
      }
    };
  }
})(window.GOVUK.Modules);

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
govuk_publishing_components-12.13.0 app/assets/javascripts/govuk_publishing_components/components/checkboxes.js
govuk_publishing_components-12.12.1 app/assets/javascripts/govuk_publishing_components/components/checkboxes.js
govuk_publishing_components-12.12.0 app/assets/javascripts/govuk_publishing_components/components/checkboxes.js
govuk_publishing_components-12.11.0 app/assets/javascripts/govuk_publishing_components/components/checkboxes.js
govuk_publishing_components-12.10.0 app/assets/javascripts/govuk_publishing_components/components/checkboxes.js